0

我有帖子类型的推荐。

我通过阅读更多链接列出了特定分类法的推荐信。

当用户单击阅读更多链接get_permalink( $post )并重定向到特定页面时,我想显示与当前帖子相同分类的上一个和下一个帖子链接?

如果您需要更多信息,请告诉我。

我已经为它添加了 true 作为第三个元素,但是没有用

previous_post_link( '%link', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'twentytwelve' ) . '</span> %title' ,true );
4

2 回答 2

2

参考这个链接

我找到了如下解决方案

去你的 single.php 文件

// Only for Testimonial

if(get_post_type( $post )=="wpm-testimonial")
{

    $terms = array_shift(get_the_terms($post->ID, 'wpm-testimonial-category'));


    // get_posts in same custom taxonomy
    $postlist_args = array(
        'posts_per_page'  => -1,
        'orderby'         => 'ID title',
        'order'           => 'ASC',
        'post_type'       => 'wpm-testimonial',
        $terms->taxonomy  => $terms->slug
    );

    $postlist = get_posts( $postlist_args );

    // get ids of posts retrieved from get_posts
    $ids = array();
    foreach ($postlist as $thepost) {
        $ids[] = $thepost->ID;
    }

    // get and echo previous and next post in the same taxonomy
    $thisindex  = array_search($post->ID, $ids);
    $previd     = $ids[$thisindex-1];
    $nextid     = $ids[$thisindex+1];

    ?>
    <nav class="nav-single">
    <?php

        if ( !empty($nextid) ) {

            echo '<span class="nav-previous"><a rel="next" href="' . get_permalink($nextid). '">Previous</a></span>';

        }

        if ( !empty($previd) ) {

    echo '<span class="nav-next"><a rel="prev" href="' . get_permalink($previd). '">Next</a></span>';

        }

    ?>
    </nav><!-- .nav-single -->

    <?php
}else{

    // Your Default Previous/Next Links in single.php file
}
?>
于 2014-10-08T14:05:40.410 回答
1

编辑: OP 改变了他的问题 - 所以我的解决方案不再正确。

我认为您可以在 Wordpress 法典中找到您的解决方案 - http://codex.wordpress.org/Template_Tags/next_post_link

同一类别中的上一篇文章(链接为文本)

 <?php previous_post_link('%link', 'Previous in category', TRUE); ?> 

同一类别中的下一篇文章(链接为文本)

<?php next_post_link( '%link', 'Next post in category', TRUE ); ?>
于 2014-10-08T11:29:00.867 回答