WP - Post images as a link to next post page - php

I am using wordpress and twenty twelve child as my theme.
I made a website for funny pictures, and basically what I want is the image inside every page to act like a link to a next image. Heres some example sites with this feature: DailyHaHa and Meme-lol.com You see if you click the image, it will take you to next image page. I'm working on local machine so I can't give link to my website. Here is my content.php:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if ( is_sticky() && is_home() && ! is_paged() ) : ?>
<div class="featured-post">
<?php _e( 'Featured post', 'twentytwelve' ); ?>
</div>
<?php endif; ?>
<div class="entry-header">
<?php the_post_thumbnail(); ?>
<?php if ( is_single() ) : ?>
<div class="post-header">
<div class="entry-title">
<?php the_title(); ?>
</div>
<span class="inline-div">
<span class="entry-bar"><li><?php echo "Added "; wp_days_ago(); ?></li> </span>
<li><?php comments_popup_link( '<span class="leave-reply">' . __( 'Be first to comment', 'twentytwelve' ) . '</span>', __( '1 comment', 'twentytwelve' ), __( '% comments', 'twentytwelve' ) ); ?></li>
<!-- .comments-link -->
<span class="edit"><li><?php edit_post_link( __( 'Edit', 'twentytwelve' ), '<span class="edit-link">', '</span>' ); ?></li></span>
</span>
<?php else : ?>
<div class="entry-title">
<?php the_title(); ?>
</div>
<span class="inline-div">
<span class="entry-bar"><li><?php echo "Added "; wp_days_ago(); ?></li> </span>
<li><?php comments_popup_link( '<span class="leave-reply">' . __( 'Be first to comment', 'twentytwelve' ) . '</span>', __( '1 comment', 'twentytwelve' ), __( '% comments', 'twentytwelve' ) ); ?></li>
<!-- .comments-link -->
<span class="edit"><li><?php edit_post_link( __( 'Edit', 'twentytwelve' ), '<span class="edit-link">', '</span>' ); ?></li></span>
</span>
<?php endif; // is_single() ?>
</div><!-- .entry-header -->
<?php if ( is_search() ) : // Only display Excerpts for Search ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
<?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentytwelve' ) ); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'twentytwelve' ), 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
<?php endif; ?>
<div class="share-buttons">
<a class="facebook-button" target="_blank" onclick="return !window.open(this.href, 'Facebook', 'width=640,height=300')" href="http://www.facebook.com/sharer/sharer.php?u=<?php the_permalink(); ?>"><img src="/images/share.png" /></a>
<a class="tweet-button" target="_blank" href="http://twitter.com/share"><img src="/images/tweet.png" /></a>
</div>
<div class="nav2">
<span class="previous-button">
<?php next_post_link( '%link', '<img src="/images/prev.png" alt="Previous"/>' ); ?>
</span>
<span class="random-post">
<?php $randomPost = $wpdb->get_var("SELECT guid FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY rand() LIMIT 1");
echo '<img src="/images/random.png" alt="Random"/>'; ?>
</span>
<span class="next-button">
<?php previous_post_link( '%link', '<img src="/images/Next.png" alt="Next"/>' ); ?>
</div>
<br />
</span>
</article><!-- #post -->

You need to get the featured image of the post first.
<?php // Display the thumbnail of the previous post ?>
<span class="previous-button"> <?php
$prevPost = get_previous_post();
$prevthumbnail = get_the_post_thumbnail($prevPost->ID); ?>
<?php previous_post_link('%link', $prevthumbnail); ?>
</span>
<?php // Display the thumbnail of the next post ?>
<span class="next-button"> <?php
$nextPost = get_next_post();
$nextthumbnail = get_the_post_thumbnail($nextPost->ID); ?>
<?php next_post_link('%link', $nextthumbnail); ?>
</span>

Related

How to display blog in a section of a page in Wordpress?

I'm trying to display blog posts underneath the 'about us' paragraph on an about page by using the code below in a template part. However, it's only returning the title of the actual page and the date info as the date I've edited the page.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<article class="post">
<header>
<h3><?php the_title(); ?></h3>
<div class="post-details">
<i class="fa fa-user"></i><?php the_author_posts_link(); ?>
<i class="fa fa-calendar"></i> <?php the_time( 'F jS, Y' ); ?>
<i class="fa fa-folder-open-o"></i> <?php the_category( ', ' ); ?>
<i class="fa fa-comments"></i><?php comments_popup_link( 'No Comments »', '1 Comment »', '% Comments »' ); ?>
</div><!-- post details -->
</header>
<div class="post-excerpt">
<p><?php the_excerpt(); ?> continue reading</p>
</div><!-- post-excerpt -->
<hr>
</article><!-- end article -->
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
What code do I need to pull my actual blog posts into this section?
In your snippet the custom query for your posts is missing. Try something like this:
// WP_Query arguments
$args = array(
'post_type' => 'post',
'post_status' => 'publish'
);
$custom_query = new WP_Query( $args );
<?php if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<article class="post">
<header>
<h3><?php the_title(); ?></h3>
<div class="post-details">
<i class="fa fa-user"></i><?php the_author_posts_link(); ?>
<i class="fa fa-calendar"></i> <?php the_time( 'F jS, Y' ); ?>
<i class="fa fa-folder-open-o"></i> <?php the_category( ', ' ); ?>
<i class="fa fa-comments"></i><?php comments_popup_link( 'No Comments »', '1 Comment »', '% Comments »' ); ?>
</div><!-- post details -->
</header>
<div class="post-excerpt">
<p><?php the_excerpt(); ?> continue reading</p>
</div><!-- post-excerpt -->
<hr>
</article><!-- end article -->
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php
// Restore original Post Data
wp_reset_postdata();
endif;
?>
Here you can find an useful tool to generate a Wordpress Query:
https://generatewp.com/wp_query/
Here you can find permitted arguments for Wordpress Query:
https://developer.wordpress.org/reference/classes/wp_query/
To use your custom query remember to call have_posts() and the_posts() methods with your query object ( $custom_query->have_posts() and $custom_query->the_post() in snippet), furthermore is important wp_reset_postdata() to restore main query.

Wordpress doesn't show excerpt when comments are off

i have a problem with my wordpress theme. It works, but when i would like to turn the comments off, it doesn't show me the excerpt.
The first picture shows how it look like when comments are allowed, the second picture shows it when comments are not allowed.
Comments allowed
Comments off
Here is the code snippet from content.php page:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<div class="entry-header-title">
<?php if ( is_single() ) : ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php else : ?>
<h1 class="entry-title">
<?php the_title(); ?>
</h1>
<?php endif; // is_single() ?>
<span class="entry-meta-date">
<?php twentythirteen_entry_date(); ?>
<span>|</span>
<?php if ( comments_open() && ! is_single() ) : ?>
<span class="entry-comments" >
<?php comments_popup_link( '<span class="leave-reply">' . __( 'Leave a Reply', 'twentythirteen' ) . '</span>', __( '1 Comment', 'twentythirteen' ), __( '% Comments', 'twentythirteen' ) ); ?>
</span><!-- .entry-comments -->
</span><!-- .entry-meta -->
<?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
</div>
</header><!-- .entry-header -->
<?php if ( is_search() ) : // Only display Excerpts for Search ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
<?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentythirteen' ) ); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-links"><div class="page-links-title">' . __( 'Pages:', 'twentythirteen' ) . '</div>', 'after' => '</div>', 'link_before' => '<div>', 'link_after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
<?php endif; ?>
<footer class="entry-meta">
<?php endif; // comments_open() ?>
<div class="entry-labels">
<?php twentythirteen_entry_meta(); ?>
</div>
<div class="entry-share">
<?php if( function_exists('ADDTOANY_SHARE_SAVE_KIT') ) { ADDTOANY_SHARE_SAVE_KIT(); } ?>
</div>
<div class=".post-spacer">
</div>
</footer><!-- .entry-meta -->
</article><!-- #post -->
<div class="linkwithin_div"></div>
You accidentally deleted an <?php endif;?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<div class="entry-header-title">
<?php if ( is_single() ) : ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php else : ?>
<h1 class="entry-title">
<?php the_title(); ?>
</h1>
<?php endif; // is_single() ?>
<span class="entry-meta-date">
<?php twentythirteen_entry_date(); ?>
<span>|</span>
<?php if ( comments_open() && ! is_single() ) : ?>
<span class="entry-comments" >
<?php comments_popup_link( '<span class="leave-reply">' . __( 'Leave a Reply', 'twentythirteen' ) . '</span>', __( '1 Comment', 'twentythirteen' ), __( '% Comments', 'twentythirteen' ) ); ?>
</span><!-- .entry-comments -->
</span><!-- .entry-meta -->
//THIS ONE
<?php endif; // comments_open() ?>
<?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
</div>
</header><!-- .entry-header -->
<?php if ( is_search() ) : // Only display Excerpts for Search ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
<?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentythirteen' ) ); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-links"><div class="page-links-title">' . __( 'Pages:', 'twentythirteen' ) . '</div>', 'after' => '</div>', 'link_before' => '<div>', 'link_after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
<?php endif; ?>
<footer class="entry-meta">
<div class="entry-labels">
<?php twentythirteen_entry_meta(); ?>
</div>
<div class="entry-share">
<?php if( function_exists('ADDTOANY_SHARE_SAVE_KIT') ) { ADDTOANY_SHARE_SAVE_KIT(); } ?>
</div>
<div class=".post-spacer">
</div>

Two-column post display on index with Bootstrap in Wordpress

Currently, I'm attempting to have a two-columned post display layout on my index page, but unfortunately, none of my tries seem to work. My idea is to have them shown like this:
Article one Article two
Article three Article four
Article five Article six
Instead, however, they're shown like this:
Article one
Article twoArticle three
And so forth. In my content.php file, the following is the code I have:
<?php
/**
* #package dazzling
*/
?>
<div class="row"> <div class="col-sm-12 col-md-6">
<div class="thumbnail" style="padding: 0px;">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="caption"><header class="entry-header page-header">
<h3><?php the_title(); ?> <small style="color: inherit;"><li class="glyphicon glyphicon-new-window"></li></small></h3>
<?php if ( 'post' == get_post_type() ) : ?>
<small class="entry-meta">
<?php dazzling_posted_on(); ?><?php if ( ! post_password_required() && ( comments_open() || '0' != get_comments_number() ) ) : ?>
<span class="comments-link"><li class="glyphicon glyphicon-comment"></li> <?php comments_popup_link( __( 'Leave a comment', 'dazzling' ), __( '1 Comment', 'dazzling' ), __( '% Comments', 'dazzling' ) ); ?></span>
<?php endif; ?>
<?php if ( 'post' == get_post_type() ) : // Hide category and tag text for pages on Search ?>
<?php
/* translators: used between list items, there is a space after the comma */
$categories_list = get_the_category_list( __( ', ', 'dazzling' ) );
if ( $categories_list && dazzling_categorized_blog() ) :
?>
<span class="cat-links"><li class="glyphicon glyphicon-folder-open"></li>
<?php printf( __( ' %1$s', 'dazzling' ), $categories_list ); ?>
</span>
<?php endif; // End if categories ?>
<?php endif; // End if 'post' == get_post_type() ?>
<?php edit_post_link( __( 'Edit', 'dazzling' ), '<li class="glyphicon glyphicon-pencil"></li> <span class="edit-link">', '</span>' ); ?>
</small><!-- .entry-meta -->
<?php endif; ?>
</header><!-- .entry-header -->
<?php if ( is_search() ) : // Only display Excerpts for Search ?>
<?php the_excerpt(); ?>
<p><a class="btn btn-default read-more" href="<?php the_permalink(); ?>"><?php _e( 'Continue reading', 'dazzling' ); ?> <span class="glyphicon glyphicon-chevron-right"></span></a></p>
</div><!-- .entry-summary -->
<?php else : ?>
<?php if ( has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>" style="float: left; margin-right: 10px;" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail( 'medium', array( 'class' => 'img-thumbnail' )); ?>
</a>
<div class="caption">
<?php the_excerpt(); ?>
</div>
<?php else : ?>
<?php the_excerpt(); ?>
<?php endif; ?>
<p><a class="btn btn-default read-more" href="<?php the_permalink(); ?>"><?php _e( 'Continue reading', 'dazzling' ); ?> <span class="glyphicon glyphicon-chevron-right"></span></a></p>
<?php
wp_link_pages( array(
'before' => '<div class="page-links">'.__( 'Pages:', 'dazzling' ),
'after' => '</div>',
'link_before' => '<span>',
'link_after' => '</span>',
'pagelink' => '%',
'echo' => 1
) );
?>
</div><!-- .entry-content -->
<?php endif; ?>
<br>
</article></div></div></div>
<!-- #post-## -->
Could you all please explain if I'm doing anything wrong here? Thank you!
The easiest solution would probably be a CSS approach. If you assign a class to each post that is printed on the page, you could then float half to the right, and half to the left. Here is an example:
Sample HTML
<div class="wrapper">
<div class="post">Sample Post 1 Content</div>
<div class="post">Sample Post 2 Content</div>
<div class="post">Sample Post 3 Content</div>
<div class="post">Sample Post 4 Content</div>
<div class="post">Sample Post 5 Content</div>
<div class="post">Sample Post 6 Content</div>
</div>
Corresponding CSS
.post:nth-child(odd) {
float:left;
width:50%;
}
.post:nth-child(even) {
float: right;
width:50%;
}
Here is a demo where you can see this approach in action.

3 column layout wordpress (3 posts on a page)

My client wants to have his news page with 3 columns and 3 posts on the page.
Each column has a title name, date, the excerpt and read more function.
Like this: http://rikvandoorn.nl/nieuws_pagina.jpg
Cause my PHP knowledge isn't that good, i was looking for some tutorials to create the 3 column page. But al those tutorials are outdated, in lack of information or they don't seem to work that well.
And the "leave a reply" can be moved to the single.php.
So i hope some of you can provide me some help.
This is my current content.php
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="aside">
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentytwelve' ) ); ?>
</div><!-- .entry-content -->
</div><!-- .aside -->
<footer class="entry-meta">
<?php echo get_the_date(); ?>
<?php if ( comments_open() ) : ?>
<div class="comments-link">
<?php comments_popup_link( '<span class="leave-reply">' . __( 'Leave a reply', 'twentytwelve' ) . '</span>', __( '1 Reply', 'twentytwelve' ), __( '% Replies', 'twentytwelve' ) ); ?>
</div><!-- .comments-link -->
<?php endif; // comments_open() ?>
<?php edit_post_link( __( 'Edit', 'twentytwelve' ), '<span class="edit-link">', '</span>' ); ?>
</footer><!-- .entry-meta -->
</article><!-- #post -->
Set up the wordpress loop
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="aside">
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentytwelve' ) ); ?>
</div><!-- .entry-content -->
</div><!-- .aside -->
<?php edit_post_link( __( 'Edit', 'twentytwelve' ), '<span class="edit-link">', '</span>' ); ?>
</article><!-- #post -->
<?php endwhile; endif; wp_reset_query(); ?>
and then in the admin go to Settings > General > Reading - select 3 in 'Blog pages show at most'

Why the single.php in wordpress theme, every page display the same content?

<div class="content" style="min-height:1022px;">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="singlepost">
<h1 class="page-title"><?php the_title(); ?> </h1>
<div class="post_info">
<p><?php the_time('Y年n月j日') ?>
<span>/ </span>
<?php comments_popup_link('0 comment', '1 comment', '% comments', '', 'closed'); ?>
<span>/ </span>
<?php the_category(', ') ?>
</p>
</div>
<div class="post1">
<div class="post1text">
<!-- Post Content -->
<?php the_content(); ?>
</div>
</div>
<p class="clearfix" style="margin-left:20px;"><?php previous_posts_link('<< TheNewer', 0); ?> <span class="float right"><?php next_posts_link('ThePast >>', 0); ?></span></p>
</div>
<?php endwhile; else : ?>
<?php endif; ?>
<div class="cleared"></div>
</div>
Above code is my single.php which display the content, but it's display every page the same content, so, what's wrong with it? is the loop? Thanks very much~
It's so easy once you understand the basic template selection hierarchy, which should get you started.
http://codex.wordpress.org/Template_Hierarchy
You can also create page templates, it's really very simple and useful.
http://codex.wordpress.org/Pages#Creating_your_own_Page_Templates
And finally once you have got all that working, you can start doing some really clever stuff with conditional tags.
http://codex.wordpress.org/Conditional_Tags
Instead of your code try this simple code in single.php
<div class="content" style="min-height:1022px;">
<?php while ( have_posts() ) : the_post(); ?>
<nav id="nav-single">
<h3 class="assistive-text"><?php _e( 'Post navigation', 'twentyeleven' ); ?></h3>
<span class="nav-previous"><?php previous_post_link( '%link', __( '<span class="meta-nav">←</span> Previous', 'twentyeleven' ) ); ?></span>
<span class="nav-next"><?php next_post_link( '%link', __( 'Next <span class="meta-nav">→</span>', 'twentyeleven' ) ); ?></span>
</nav><!-- #nav-single -->
<?php get_template_part( 'content', 'single' ); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
</div>

Categories