Support » Fixing WordPress » How to display prev/next posts’ category names?

  • Resolved ymcheung

    (@ymcheung)


    I have done prev/next in single.php:

    <?php the_post_navigation
    ( 
        array(
            'prev_text'          => '<p>Previous:</p><p>%title</p>',
            'next_text'          => '<p>Next:</p><p>%title</p>'
        ) 
    );

    Is it possible to show category names of prev/next posts? (Didn’t find anything about this in WordPress Codex)

    It would be like:

    Previous: CategoryA – An Old Post Title
    Next: CategoryB – Another Post Title

    Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hey there,

    Hope you are doing great! 🙂

    You can use the following code to get category names:

    the_post_navigation( array(
                'prev_text'                  => __( 'prev chapter: %title' ),
                'next_text'                  => __( 'next chapter: %title' ),
                'in_same_term'               => true,
                'taxonomy'                   => __( 'category' ),
                'screen_reader_text' => __( 'Continue Reading' ),
            ) );

    Also, I would suggest you to check these following links:
    https://codex.wordpress.org/Function_Reference/previous_post_link
    https://codex.wordpress.org/Next_and_Previous_Links

    https://chriswharton.me/2014/03/category-based-previous-and-next-links-on-custom-post-types-for-wordpress/

    Let me know if this helps!

    Regards,
    Archana.

    Hello Archana,

    Thanks for the reply!
    Showing posts within a category is very convenient.

    However, I would like to show the category names in the previous/next post navigation.

    Previous: CategoryA – An Old Post Title
    Next: CategoryB – Another Post Title

    The navigation won’t be limited in one category. Therefore, I want to show their category names as well.

    Hey ymcheung,

    Seems interesting post, so i have tried some code at my local, and this seems to be working for me

    
    $prev_post = get_previous_post();
    					if (!empty( $prev_post )): 
    						$previd = $prev_post->ID;
    						$prev_cats = wp_get_post_categories( $previd, array('fields'=>'names') );
    
    						if(!empty($prev_cats)){
    							echo '<div style="float:left;width:50%;">';
    							echo "Previous: ".implode(',', $prev_cats).' - <a href="'.$prev_post->guid.'">'. $prev_post->post_title .'</a>';	
    							echo '</div>';
    						}
    						?>
    					  
    					<?php endif;
    
    					$next_post = get_next_post();
    					if (!empty( $next_post )): 
    					  $nextid = $next_post->ID;
    						$next_cats = wp_get_post_categories( $nextid, array('fields'=>'names') );
    
    						if(!empty($next_cats)){
    							echo '<div style="float:right;width:50%;">';
    							echo "Next: ".implode(',', $next_cats).' - <a href="'.$next_post->guid.'">'. $next_post->post_title .'</a>';	
    							echo '</div>';
    						}
    					 endif; ?>
    

    put this code in your single.php where you need navigations to appear. Let me know if this works for you.

    Thanks,
    Swayam

    Hello swayamtejwani,

    It works pretty well!

    May I post your code in the Code Reference for those who may find this useful in the future?
    https://developer.wordpress.org/reference/functions/get_previous_post/

    Of course I’ll note that it’s from you.

    Thank you!

    • This reply was modified 9 months, 2 weeks ago by  ymcheung. Reason: grammar fix

    Sure sir, not a problem, glad it works for you 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to display prev/next posts’ category names?’ is closed to new replies.