图片待添加
图片待添加

WordPress主题开发教程:文章详细页面调用文章的上一篇和下一篇文章

2020-02-20 19:24 分类:WordPress主题开发制作教程 阅读(次阅读) 标签:,

WordPress主题的文章详细页面的底部一般会调用上一篇文章与下一篇文章.

WordPress主题开发教程:文章详细页面调用当前文章的上一篇和下一篇文章

之前写了篇: WordPress主题开发教程:文章详细页面调用当前文章的前5篇和后5篇文章
这样访客可能会看到他感兴趣的内容,会继续浏览下去:

代码如下,加在WordPress主题的文章详细页面的底部:

<div class="single_next">
   <ul>
    <li><?php if (get_previous_post($categoryIDS)) { previous_post_link('上一篇: %link','%title',true);} else { echo "没有了,已经是末尾文章";} ?> </li>
    <li><?php if (get_next_post($categoryIDS)) { next_post_link('上一篇: %link','%title',true);} else { echo "没有了,已经是新文章";} ?> </li>
   </ul>
</div>

请自行修改为你想要的WordPress主题的样式;

比如给上一篇文章和下一篇文章加上缩略图:

<?php
    $current_category=get_the_category();
    $prev_post = get_previous_post($current_category,'');
    $next_post = get_next_post($current_category,'');
?>
<div class="previous_post_link fl">
    <?php if (!emptyempty( $prev_post )): ?>
        <a href="<?php echo get_permalink( $prev_post->ID ); ?>"><?php echo get_the_post_thumbnail( $prev_post->ID, '', '' ); ?></a>
 上一篇: <a href="<?php echo get_permalink( $prev_post->ID ); ?>"><?php echo $prev_post->post_title; ?></a>
    <?php endif; ?>
</div>
<div class="next_post_link fr">
    <?php if (!emptyempty( $next_post )): ?>
        <a href="<?php echo get_permalink( $next_post->ID ); ?>"><?php echo get_the_post_thumbnail( $next_post->ID, '', '' ); ?></a>
 上一篇: <a href="<?php echo get_permalink( $next_post->ID ); ?>"><?php echo $next_post->post_title; ?></a>
    <?php endif; ?>
</div>

但是这个调用上一篇文章,调用下一篇文章是整站的,如何调用当前分类的上一篇与下一篇文章:

文章详细页面调用当前分类的上一篇文章与下一篇文章

<?php
    $categories = get_the_category();
    $categoryIDS = array();
    foreach ($categories as $category) {
        array_push($categoryIDS, $category->term_id);
    }
    $categoryIDS = implode(",", $categoryIDS);
?>
<?php if (get_previous_post($categoryIDS)) { previous_post_link('上一篇: %link','%title',true);} else { echo "没有了,已经是末尾文章";} ?>
<?php if (get_next_post($categoryIDS)) { next_post_link('上一篇: %link','%title',true);} else { echo "没有了,已经是新文章";} ?>

你也可以试试这个

文章详细页面调用当前分类的上一篇文章与下一篇文章方法2:

<?php
    $current_category=get_the_category();
    $prev_post = get_previous_post($current_category,'');
    $next_post = get_next_post($current_category,'');
?>
<div class="next-left">
    <?php if (!emptyempty( $prev_post )): ?>
        <span>上一篇:<a title="<?php echo $prev_post->post_title; ?>" href="<?php echo get_permalink( $prev_post->ID ); ?>" rel="prev" ><?php echo $prev_post->post_title; ?></a></span>
    <?php else: ?>
        <span>上一篇:没有了,已经是刚刚发布的文章了</span>
    <?php endif; ?>
</div>
<div class="next-right">
    <?php if (!emptyempty( $next_post )): ?>
        <span>下一篇:<a title="<?php echo $next_post->post_title; ?>" href="<?php echo get_permalink( $next_post->ID ); ?>" rel="next" ><?php echo $next_post->post_title; ?></a></span>
    <?php else: ?>
        <span>上一篇:没有了,已经是刚刚发布的文章了</span>
    <?php endif; ?>
</div>

本文地址: https://www.wpyi.com/last-article.html