php – WordPress next_post_link / previous_post_link不保持在同一类别

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – WordPress next_post_link / previous_post_link不保持在同一类别脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的设置.

single.PHP

<?PHP

    if ( in_category( 'my-category' ) ) {
        include( TEMPLATEPATH.'/single-my-category.PHP' );
    }
    else {
        include( TEMPLATEPATH.'/single-generic.PHP' );
    }
?>

单我-category.PHP

<?PHP

if ( have_posts() ) : while (have_posts()) : the_post(); ?>

<?PHP echo the_tITle(); ?>

<div class="pagination">
    <div class="container">
        <div class="row">
            <div class="next col-xs-6 col-sm-6 col-md-6 col-lg-6">
                <?PHP next_post_link( '%link','<img src="' . get_template_directory_uri() . '/img/cs-left.png" /> PrevIoUS',true ); ?>
            </div>

            <div class="PRevIoUs col-xs-6 col-sm-6 col-md-6 col-lg-6">
                <?PHP prevIoUs_post_link( '%link','NEXT <img src="' . get_template_directory_uri() . '/img/cs-right.png" />',true ); ?>
            </div>
        </div>
    </div>
</div>

<?PHP endwhile; endif; ?>

这就是我所追踪的 – http://codex.wordpress.org/Function_Reference/next_post_link

我不太清楚我在这里做错了什么,因为某些原因,即使函数的in_same_term参数设置为true,prevIoUs_post_link也会将我转到不同类别的帖子.

有任何想法吗?

谢谢.

编辑你的代码块如下.你没有把’.PHP’放在你的single.PHP文件的第5行.上一个/下一个帖子将仅显示您指定的类别的职位,其中if语句为single.PHP(此处为“PHP”).对于以下示例,我创建了一个目录“template-parts”,并在该目录中创建了两个PHP文件(“single-my-category.PHP”和“single-generic.PHP”).

single.PHP

<?PHP
    $the_query = new WP_Query( $post );

    if (in_category('PHP')) {
        include( 'template-parts/single-my-category.PHP' );
    } else {
        include( 'template-parts/single-generic.PHP' );
    }
?>

单我-category.PHP

if ( have_posts() ) : while (have_posts() ) : the_post(); ?>

<?PHP the_title(); ?>

<div class="pagination">
    <div class="container">
        <div class="row">
            <div class="next col-xs-6 col-sm-6 col-md-6 col-lg-6">
                <?PHP next_post_link( '%link',true ); ?>
            </div>
        </div>
    </div>
</div>

<?PHP endwhile; endif; ?>

脚本宝典总结

以上是脚本宝典为你收集整理的php – WordPress next_post_link / previous_post_link不保持在同一类别全部内容,希望文章能够帮你解决php – WordPress next_post_link / previous_post_link不保持在同一类别所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。