categories - get_next_post set the category

admin2025-01-07  7

Is it possible in Wordpress to set the category used in get_next_post?

I have post that can have more than one category.

On the single.php page showing the post I want to show next and prev posts.

I have the category I want to choose from on the single.php so how do I use that in get_next_post

    $the_cat = 'latest';

    $next_post = get_next_post($in_same_term = true, $excluded_terms = '', $taxonomy = $the_cat); 

Is it possible in Wordpress to set the category used in get_next_post?

I have post that can have more than one category.

On the single.php page showing the post I want to show next and prev posts.

I have the category I want to choose from on the single.php so how do I use that in get_next_post

    $the_cat = 'latest';

    $next_post = get_next_post($in_same_term = true, $excluded_terms = '', $taxonomy = $the_cat); 
Share Improve this question edited Apr 11, 2016 at 21:28 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Apr 11, 2016 at 20:52 user668499user668499 3518 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Try this:

$the_cat = 'latest';
$term_args = array('orderby' => 'term_id', 'order' => 'ASC', 'fields' => array('term_id', 'slug'));
$terms = wp_get_post_terms( $post->ID, 'category', $term_args );
foreach($terms as $term){
    if($term->slug !== $the_cat){
        $terms_to_exclude .= $term->term_id.',';
    }
}
$next_post = get_next_post($in_same_term = true, $excluded_terms = $terms_to_exclude, $taxonomy = 'category');
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736257228a422.html

最新回复(0)