简体   繁体   English

WordPress如何从同一分类法给上一个和下一个帖子链接?

[英]Wordpress How to give previous and next post link from same taxonomy?

I have post type testimonials. 我有职位类型的推荐。

I am listing that testimonials by specific taxonomy with read more link. 我按特定分类列出了该推荐,并提供了更多链接。

When user click on read more link which is get_permalink( $post ) , and redirect to specific page then i want to show previous and next post link with same taxonomy of current post? 当用户单击阅读更多链接get_permalink( $post )并重定向到特定页面时,我想显示具有与当前帖子相同分类法的上一个和下一个帖子链接?

If you require any more info then let me know. 如果您需要更多信息,请告诉我。

I have added true as third element for it, but don't worked 我将true添加为第三个元素,但是没有用

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

Refer this Link 引用此链接

I found solution as below 我发现解决方案如下

Go your single.php file 去你的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
}
?>

EDIT: OP changed his question - so my solution is no longer correct. 编辑: OP改变了他的问题-所以我的解决方案不再正确。

I think you can find your solution in the Wordpress Codex - http://codex.wordpress.org/Template_Tags/next_post_link 我认为您可以在Wordpress Codex中找到您的解决方案-http: //codex.wordpress.org/Template_Tags/next_post_link

Previous post within same category (Link as text) 同一类别中的上一篇文章(链接为文本)

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

Next post within same category (Link as text) 同一类别下的下一篇文章(链接为文本)

<?php next_post_link( '%link', 'Next post in category', TRUE ); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM