WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Daniel Flodin
    Member
    Posted 3 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 vanDoorn
    Member
    Posted 3 years ago #

    Hi. Found this. Hope it helps. Peter

    http://wordpress.org/support/topic/how-to-get-next-post-id

  3. alchymyth
    Forum Moderator
    Posted 3 years ago #

    example:

    <?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; }
    endif;

    analogue for previous post

    http://codex.wordpress.org/Function_Reference/get_next_post
    http://codex.wordpress.org/Function_Reference/get_the_category

  4. Daniel Flodin
    Member
    Posted 3 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