TSYBLOG.COM

WordPress文章上一篇下一篇带显示缩略图

效果图

%title插图%num

代码:

 //WordPress文章上一篇下一篇显示缩略图
function wptoo_pageturn_thumb($id){
 if (has_post_thumbnail($id)) {
 echo get_the_post_thumbnail( $id, '', '' );
 } else {
 $first_img = '';
 ob_start();
 ob_end_clean();
 $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', get_post( $id )->post_content, $matches);
 $first_img = $matches [1] [0];
 if(empty($first_img)){ //Defines a default image
 $random = mt_rand(1, 10);
 $first_img= get_bloginfo ( 'stylesheet_directory' ).'/images/random/'.$random.'.jpg';
 }
 echo '<img class="uk-overlay-scale" src="'.$first_img.'" alt="'.get_post( $post_id )->post_title.'" />';
 }
} 

使用方法

1、首先将上面的代码按需要添加到主题 functions.php 文件中。

2、然后将下面代码添加到single.php文件中,文章上一篇下一篇显示缩略图就能够实现了。

 <?php
$current_category = get_the_category();//获取当前文章所属分类ID
$prev_post = get_previous_post($current_category,'');//与当前文章同分类的上一篇文章
$next_post = get_next_post($current_category,'');//与当前文章同分类的下一篇文章
?>
<div>
 <?php if (!empty( $prev_post )): ?>
 <?php wptoo_pageturn_thumb($prev_post->ID);?>
 <a href="<?php echo get_permalink( $prev_post->ID ); ?>" rel="external nofollow"  >
 <span>上一篇:<?php echo $prev_post->post_title; ?><span>
 </a>
 <?php endif; ?>
</div>
<div>
 <?php if (!empty( $next_post )): ?>
 <?php wptoo_pageturn_thumb($next_post->ID);?>
 <a href="<?php echo get_permalink( $next_post->ID ); ?>" rel="external nofollow"  >
 <span>下一篇:<?php echo $next_post->post_title; ?></span>
 
 </a>
 <?php endif; ?>
</div> 

喜欢本文?
文章阅读
常见问题
免费下载或者VIP会员专享资源能否直接商用?
提示下载完成但无法解压?
投稿及掉连解决方法:

本站均测试后上架,保证所提供下载的资源的准确性、安全性和完整性。

如有链接无法下载、失效或广告,下方评论区留言,或联系客服处理,看到处理!

如果您也有好的资源或教程,您可以投稿发布,所得成本报酬归发布方所有!

发表评论