Can I make the next/previous post be in the same category as the current post?

Home Support Blog Way Can I make the next/previous post be in the same category as the current post?

Tagged: 

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #13731
    Anonymous
    Inactive

    I have three blogs on my site, http://www.katiesonger.com, and I’m using categories to separate them. (www.katiesonger.com/writing is one category, http://www.katiesonger.com/politics is another, etc.) On each of these three category blogs, I’d like the next/previous post buttons to point to the next/previous post in the same category, rather than the next/previous post across all categories.

    I’ve found many queries about this, and the answers all say that I need to work with the code next_post_link and _prev_post_link, adding the argument in_same_term somewhere in the code. I’m new to coding, but I did create a child theme, and I’ve found the Loop code in index.php–Wordpress’ documentation seems to say that the code should be placed somewhere there (here’s their documentation). I’m still not sure, though, where to place the new code… Can you help me? Or is there some easier way to do this in Blog Way? Thanks very much!

    #13734
    Anonymous
    Inactive
    #13784
    nirav
    Keymaster

    Hello Katie,

    Greetings of the day!

    It might be helpful.

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

    Please refer this link for more details.

    Please let me know in case you need more help!

    Thanks.

    #13806
    Anonymous
    Inactive

    Hi there,

    Thanks very much for this info. I’m still a little confused: I see the code I need to use, and I see that the link you sent says I should put this code in single.php, but I don’t know where I should put the new code into the single.php code.

    Here is the entire code for single.php:

    <?php
    /**
     * The template for displaying all single posts.
     *
     * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
     *
     * @package Blog_Way
     */
    
    get_header(); 
    
    	/**
    	 * Hook - blog_way_before_primary.
    	 *
    	 * @hooked blog_way_before_primary_action - 10
    	 */
    	do_action( 'blog_way_before_primary' );
    ?>
    
    	<?php
    	while ( have_posts() ) : the_post();
    
    		get_template_part( 'template-parts/content', 'single' );
    
    		do_action( 'blog_way_related_post' );
    
    		do_action( 'blog_way_author_info' );
    
    		//Load up the navigation function if it is enabled.
    		$show_nav = blog_way_get_option( 'show_post_nav' );
    
    		if ( 1 == $show_nav ) :
    			the_post_navigation();
    		endif;
    
    		// If comments are open or we have at least one comment, load up the comment template.
    		if ( comments_open() || get_comments_number() ) :
    			comments_template();
    		endif;
    
    	endwhile; ?>
    
    <?php
    	/**
    	 * Hook - blog_way_after_primary.
    	 *
    	 * @hooked blog_way_after_primary_action - 10
    	 */
    	do_action( 'blog_way_after_primary' );
    
    	/**
    	 * Hook - blog_way_sidebar.
    	 *
    	 * @hooked blog_way_sidebar_action - 10
    	 */
    	do_action( 'blog_way_sidebar' );
    
    get_footer();

    Can you tell me where I should insert the new code? Thanks very much!

    #13808
    nirav
    Keymaster

    Hello Katie,

    Greetings of the day!

    Add below code

    if ( 1 == $show_nav ) : ?>
    	<div class="alignright"><?php next_post_link( '%link', 'Next post', TRUE ); ?></div>
    	<div class="alignleft"><?php previous_post_link('%link', 'Previous post', TRUE); ?></div>
    <?php
    Endif;

    In place of this code

    if ( 1 == $show_nav ) :
    	the_post_navigation();
    endif;

    Hope it will work for you.

    Regards,
    Team ProDesigns

    #13814
    Anonymous
    Inactive

    Thank you! This partly worked, but there is still one problem.

    Here is the code I’ve inserted, combining your instructions with the WordPress codex instructions found here:

    	if ( 1 == $show_nav ) : ?>
    	<div class="alignright"><?php next_post_link( '%link', '%title', TRUE ); ?></div>
    	<div class="alignleft"><?php previous_post_link('%link', '%title', TRUE); ?></div>
    <?php
    Endif;

    The remaining problem is that the next/previous post links are excluding parent categories; right now they are only including child categories. I’m guessing this is because I am using category pages? Maybe the next_post_link is ignoring the parent categories because each blog is already located on a parent category page?

    How can I modify this code, or use other code somewhere else, to clarify that I want next_post_link and previous_post_link to use PARENT categories?

    Thanks very much!
    Katie

    #13815
    Anonymous
    Inactive

    One more question: Previously, the next/previous post links appeared as a black button. Now they are only appearing as links. Is it possible to make them appear as buttons, so they match the theme better? Thanks!

    #13816
    Anonymous
    Inactive

    And finally, a couple more questions:

    – I wanted the next/previous post links to appear BEFORE the Related Posts, so I moved them in single.php. But now there is no space (no line break) between the next/previous post links and the Related Posts. How can I make space between them?

    – Some of my blog post titles are very long. When that occurs, the next/previous post links interfere with each other, moving each other up or down on the page instead of wrapping the long title. How can I wrap it?

    So to summarize, my questions are:

    1) How do I include parent categories in the next/previous post links?
    2) How can I make the next/previous post links appear as a black button with white text, as in the theme?
    3) How do I add space after the next/previous post links, so that there is space between them and Related Posts?
    4) How can I ensure that the text of long titles will be wrapped in the next/previous post links, so that they will not move the other link up or down the page?

    Thank you!
    Katie

    #13819
    Anonymous
    Inactive

    Update: I’ve just changed the blog links for each individual blog, removing “category” from the slug–so the updated links are:

    http://www.katiesonger.com/writing
    http://www.katiesonger.com/health
    http://www.katiesonger.com/politics

    Also, I thought of one possible answer to my main question (#1 above). I realized that I had not checked those parent categories for the posts–-so the posts were not assigned to the parents, only to the child categories. I had thought that because each child category was already inside a parent category, I did not need to assign the posts to the parents also. And I didn’t want every post in each blog to say the parent category–for example, I didn’t want all the health posts to say “Gut Feelings” as a category, because ALL of them are in that parent category, so it’s not useful to readers. However, for now, I did assign each post to its parent category, just so that the link buttons will work. They are working…but I would still like to know if there is a better way to do this.

    And I am still wondering how to answer number 2, 3, and 4 above. 🙂

    #13873
    nirav
    Keymaster

    Hello Katie,

    Hope you are doing good!

    I have forwarded the details to the technical team, will check the details and will share with you the updates in case there are in-scope updates else we will guide about how to perform it.

    Have a great day ahead!

    Thanks and Regards,
    Team ProDesigns

    #14111
    Anonymous
    Inactive

    Hi Team ProDesigns,

    Thanks for forwarding this to your technical team. I thought I’d check back in – have there been any solutions generated?

    Thanks,
    Kate

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.