The following tutorial column of WordPress will introduce to you how to add thumbnails to the previous and next article links in WordPress. I hope it will be helpful to friends who need it!
Add thumbnails for WordPress previous and next article links
Most WordPress themes will add links to previous and next articles under the text. You can add this link through the following code Add a thumbnail to make it stand out.
Add the following code to the appropriate location of the text template file.
Code 1
The featured image of the article is called 100×100 by default.
<div id="post-nav" class="navigation"> <?php $prevPost = get_previous_post(true); if($prevPost) ?> <div class="nav-box previous"> <?php previous_post_link('« « Previous Post:', 'yes'); ?> <?php $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(100,100) );?> <?php previous_post_link('%link',"$prevthumbnail <p>%title</p>", TRUE); ?> </div> <?php $nextPost = get_next_post(true); if($nextPost) ?> <div class="nav-box next" style="float:right;"> <?php previous_post_link('» » Next Post:', 'yes'); ?> <?php $nextthumbnail = get_the_post_thumbnail($nextPost->ID, array(100,100) ); ?> <?php next_post_link('%link',"$nextthumbnail <p>%title</p>", TRUE); ?> </div> <?php ?> </div>
Supporting style
#post-nav{clear: both; height: 100px; margin: 0 0 70px;} #post-nav .nav-box{background: #e9e9e9; padding: 10px;} #post-nav img{float: left; margin: 0 10px 0 0;} #post-nav p{margin: 0 10px; font-size: 11px; vertical-align: middle;} #post-nav .previous{float: left; vertical-align: middle; width: 300px; height: 120px;} #post-nav .next{float: right; width: 300px; height: 120px;}
Code 2
In addition to calling the featured image and displaying the publication time of the article, a slight modification You can also add more article information, including customized thumbnails, article briefs, etc.
<div id="post-nav"> <?php $prevPost = get_previous_post(true); if($prevPost) { $args = array( 'posts_per_page' => 1, 'include' => $prevPost->ID ); $prevPost = get_posts($args); foreach ($prevPost as $post) { setup_postdata($post); ?> <div class="post-previous"> <a class="previous" href="<?php the_permalink(); ?>">« Previous Story</a> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a> <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4> <small><?php the_date('F j, Y'); ?></small> </div> <?php wp_reset_postdata(); } //end foreach } // end if $nextPost = get_next_post(true); if($nextPost) { $args = array( 'posts_per_page' => 1, 'include' => $nextPost->ID ); $nextPost = get_posts($args); foreach ($nextPost as $post) { setup_postdata($post); ?> <div class="post-next"> <a class="next" href="<?php the_permalink(); ?>">Next Story »</a> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a> <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4> <small><?php the_date('F j, Y'); ?></strong> </div> <?php wp_reset_postdata(); } //end foreach } // end if ?> </div>
The above is the detailed content of How to add thumbnails to previous and next article links in WordPress. For more information, please follow other related articles on the PHP Chinese website!