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

用惯了文章标题+文章链接的上一篇下一篇样式吗?

前段时间博主在朋友的WordPress博客看到文章缩略图+文章标题+文章链接的上一篇下一篇样式,觉得特别有意思,于是自己也试着实现这样的效果。

网上案例也挺多的,搜出一大堆,整理了下今天分享出来

效果图

函数代码

<code>
//WordPress文章上一篇下一篇显示缩略图
function wp<span class="xiomao-430-mypl" data-mark="seo"></span>too_pagetur<span class="xiomao-441-mypl" data-mark="seo"></span>n_thumb($id){
if (has_post_thumbnail($id)) {
echo get_the_post_thumbnail( $id, '', '' );
} else {
$first_img = '';
ob_start();
ob_end_clea<span class="xiomao-585-mypl" data-mark="seo"></span>n();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', get_post( $id )->post_content,<span class="xiomao-704-mypl" data-mark="seo"></span> $matches);
$first_img = $match<span class="xiomao-735-mypl" data-mark="seo"></span>e<span class="xiomao-736-mypl" data-mark="seo"></span>s [1] [0];
if(empty(<span class="xiomao-756-mypl" data-mark="seo"></span>$first_im<span class="xiomao-765-mypl" data-mark="seo"></span>g)){ //De<span class="xiomao-774-mypl" data-mark="seo"></span>fines a default image
$ra<span class="xiomao-799-mypl" data-mark="seo"></span>ndom = mt_rand(1, 10);
$first_img= get_bl<span class="xiomao-840-mypl" data-mark="seo"></span>oginfo ( 'stylesheet_directory<span class="xiomao-871-mypl" data-mark="seo"></span>' ).'/images/random/'.$random.'.jpg';
}
echo '<img class="uk-overlay-scale" sr<span class="xiomao-960-mypl" data-mark="seo"></span>c="'.$first_img.'" alt="'.get_post( $post_id )->pos<span class="xiomao-1020-mypl" data-mark="seo"></span>t_title.'" />';
}
}</code>

使用方法

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

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

<code><?php
$current_category = get_the_category();//获取当前文章所属分类ID
$p<span class="xiomao-1300-mypl" data-mark="seo"></span>rev_post = get_previous_post($current_category,'');//与当前文章同分类的上一篇文章
$next_post = get_next_post($curren<span class="xiomao-1404-mypl" data-mark="seo"></span>t_category,'');//与当前文章同分类的下一篇文章
?>
<div>
<?php<span class="xiomao-1464-mypl" data-mark="seo"></span> if (!empty( $prev_post )): ?>
<?php wptoo_pagetu<span class="xiomao-1519-mypl" data-mark="seo"></span>rn_thumb($prev_post->ID);?&gt<span class="xiomao-1551-mypl" data-mark="seo"></span>;
<a href=<span class="xiomao-1564-mypl" data-mark="seo"></span>"https://www.nuanda<span class="xiomao-1584-mypl" data-mark="seo"></span>o.cn/<?p<span class="xiomao-1595-mypl" data-mark="seo"></span>hp echo get_permalink( $prev_post->ID ); ?>" rel<span class="xiomao-1650-mypl" data-mark="seo"></span>="nofollow"  rel="external nofollow"  rel="external nofollow"  >
<<span class="xiomao-1728-mypl" data-mark="seo"></span>span>上一篇:<?php echo $prev_post->post_title; ?<span class="xiomao-1782-mypl" data-mark="seo"></span>><span>
</a>
<?php endif; ?<span class="xiomao-1827-mypl" data-mark="seo"></span>>
</div>
<div>
<?php if (!empty( $next_post )): ?>
<?php wptoo_pageturn_thumb($next_post->ID);?>
<a href="https://www.nuand<span class="xiomao-1984-mypl" data-mark="seo"></span>ao.cn/<?php echo get_permalink( $next_post->ID ); ?>" rel="nofol<span class="xiomao-2059-mypl" data-mark="seo"></span>low"  rel="external nofollow"<span class="xiomao-2091-mypl" data-mark="seo"></span>  rel="external nofollo<span class="xiomao-2115-mypl" data-mark="seo"></span>w<span class="xiomao-2116-mypl" data-mark="seo"></span>"  >
<span>下一篇:<?php echo $next_post->post_title; ?></span>
</a>
<?php endif; ?>
</div></code>

该文章转载来自暖岛网

人已赞赏
wordpress教程网站教程

WordPress纯代码实现前台登录功能

2020-10-28 22:02:38

wordpress教程网站教程

WordPress获取文章摘要the_excerpt函数详解

2020-10-28 22:02:44

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索