[resolved] Check category ID/name of next/previous posts in a loop…? (4 posts)

  1. Daniel Flodin
    Posted 4 years ago #

    Is there any basic way I can check the category ID/name of next/previous posts in a loop and then use the information gathered in an if… else statement? I've had a quick look at get_adjacent_post(), but I can't really seem to figure out how to implement it in the way I'd want it.

  2. Peter Hardy-vanDoorn
    Posted 4 years ago #

    Hi. Found this. Hope it helps. Peter


  3. Michael
    Part-Time Forum Moderator
    Posted 4 years ago #


    <?php $next = get_next_post();
    if( $next ) :
    $next_id = $next->ID;
    	echo 'next post id: '.$next_id.'<br/>';
    $next_cat = get_the_category($next_id);
    $next_cat_id = $next_cat[0]->term_id; //'first' category only
    	echo 'next post cat id: '.$next_cat_id.'<br/>';
    //in case you need to know all categories of the adj post:
    foreach($next_cat as $cat) { echo $cat->term_id; }

    analogue for previous post


  4. Daniel Flodin
    Posted 4 years ago #

    Thanks Peter!

    …Oh, and thank you alchymyth! That's exactly what I was looking for! Now that I know it all seems a bit ridiculous how I didn't see get_next_post() in the codex from start… But, well, now everything just fell a little bit more into place. Again, thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic