Site logo

[Support request] Display post navigation to show PREVIOUS and NEXT for particular category

Home Forums Support [Support request] Display post navigation to show PREVIOUS and NEXT for particular category

Home Forums Support Display post navigation to show PREVIOUS and NEXT for particular category

Viewing 15 posts - 1 through 15 (of 26 total)
  • Author
    Posts
  • #1750515
    Daniel

    In Display Post Navigation, how do I replace the post titles with “Previous” and “Next”.

    I would like to apply this for a particular post category and keep the post titles navigation for the other categories.

    Thank you!

    #1751022
    David
    Staff
    Customer Support

    Hi there,

    try adding this PHP snippet:

    add_filter( 'generate_post_navigation_args', function( $args ) {
        if ( is_single() && ! in_category('your_category_slug') ) {
            // Display default post navigation if NOT in category
            $args['previous_format'] = '<div class="nav-previous">' . generate_get_svg_icon( 'arrow-left' ) . '<span class="prev" title="' . esc_attr__( 'Previous', 'generatepress' ) . '">%link</span></div>';
            $args['next_format'] = '<div class="nav-next">' . generate_get_svg_icon( 'arrow-right' ) . '<span class="next" title="' . esc_attr__( 'Next', 'generatepress' ) . '">%link</span></div>';
        }
        elseif ( is_single() && in_category('your_category_slug') ) {
            // Display Next Previous on specifc category
            $args['previous_format'] = '<div class="nav-previous"><span class="prev" title="' . esc_attr__( 'Previous', 'generatepress' ) . '">Previous</span></div>';
            $args['next_format'] = '<div class="nav-next"><span class="next" title="' . esc_attr__( 'Next', 'generatepress' ) . '">Next</span></div>';
        }
    
        return $args;
    } );

    You will see there are two conditions where you need to replace your_category_slug – that will be the category for where you want to change the label in both instances.

    #1751530
    Daniel

    Thanks very much David.
    Replacing links with PREVIOUS and NEXT worked for the particular category, however the links don’t work? You click on the link and nothing happens?

    #1751828
    Elvin
    Staff
    Customer Support

    Hi there,

    Can you link us to a sample page in question? So we could inspect the links for any issues.

    #1751908
    Daniel
    #1752317
    Elvin
    Staff
    Customer Support

    Thanks.

    I think that’s because the elseif didn’t have the %link so there’s no link rendered.

    Try this:

    add_filter( 'generate_post_navigation_args', function( $args ) {
    	$prev_post = get_adjacent_post( true, '', true, 'category' );
    	$next_post = get_adjacent_post( true, '', false, 'category' );
    	
        if ( is_single() && ! in_category('featured') ) {
            // Display default post navigation if NOT in category
            $args['previous_format'] = '<div class="nav-previous">' . generate_get_svg_icon( 'arrow-left' ) . '<span class="prev" title="' . esc_attr__( 'Previous', 'generatepress' ) . '">%link</span></div>';
            $args['next_format'] = '<div class="nav-next">' . generate_get_svg_icon( 'arrow-right' ) . '<span class="next" title="' . esc_attr__( 'Next', 'generatepress' ) . '">%link</span></div>';
        }
        elseif ( is_single() && in_category('featured') ) {
            // Display Next Previous on specifc category
            $args['previous_format'] = '<div class="nav-previous"><span class="prev" title="' . esc_attr__( 'Previous', 'generatepress' ) . '"><a href="'.get_permalink( $prev_post->ID ).'">Previous</a></span></div>';
            $args['next_format'] = '<div class="nav-next"><span class="next" title="' . esc_attr__( 'Next', 'generatepress' ) . '"><a href="'.get_permalink( $next_post->ID ).'">Next</a></span></div>';
        }
    
        return $args;
    } );

    Change featured to your category slug.

    #1752729
    Daniel

    Thanks heaps Elvin – I think we are nearly there.
    It works great but the only thing is the Next and Previous links are the wrong way around?
    When I click on Previous the next post opens up and when I click on Next the previous post opens up?
    I use Post types order plugin to order the posts in the archive.
    Is there a way for the Next and Previous links to correspond with the archive menu order?
    I really appreciate your great help.

    #1754327
    Elvin
    Staff
    Customer Support

    Thanks heaps Elvin – I think we are nearly there.
    It works great but the only thing is the Next and Previous links are the wrong way around?
    When I click on Previous the next post opens up and when I click on Next the previous post opens up?
    I use Post types order plugin to order the posts in the archive.
    Is there a way for the Next and Previous links to correspond with the archive menu order?
    I really appreciate your great help.

    It looks like as WordPress intended.

    “Previous” points to the “older” adjacent post. “Next” points to the “newer” adjacent post.

    But if you wish to change that, Swap the values.

    Try modifying this:

    $prev_post = get_adjacent_post( true, '', true, 'category' );
    	$next_post = get_adjacent_post( true, '', false, 'category' );

    into this:

    $prev_post = get_adjacent_post( true, '', false, 'category' );
    	$next_post = get_adjacent_post( true, '', true, 'category' );
    #1754357
    Daniel

    Thanks Elvin. It now works correctly for Next/Previous links.
    But still wrong way around for post links? Is this able to be changed around as well?

    #1754498
    Elvin
    Staff
    Customer Support

    But still wrong way around for post links? Is this able to be changed around as well?

    I’m not sure what you mean. Is there any specific condition you want the adjacent posts to be?

    Like, for example, you only want adjacent posts from the same category?

    Let us know.

    #1766996
    Daniel

    Hi Elvin sorry for delay I have been away.
    Thats right I do want adjacent posts from same category please?
    So Previous/Next for one category (which correctly displays in the menu order)
    And the post link titles for the other category (incorrectly displaying in the menu in reverse order).

    For example in the Services category the first 3 posts I have in menu order are;
    MSCL fittings
    MSCL site welding
    Stainless Steel

    On bottom of MSCL site welding post the navigation should be;
    < MSCL fittings Stainless Steel >

    Instead it is the other way around;
    < Stainless Steel MSCL fittings >

    #1768147
    Elvin
    Staff
    Customer Support

    Can you try adding this filter? so the post stays within its category

    add_filter('generate_category_post_navigation','__return_true');

    #1768573
    Daniel

    Thanks Elvin works great now with navigation staying within same category.
    I appreciate your fantastic support.

    #1771851
    Elvin
    Staff
    Customer Support

    No problem. Glad you got it sorted. 🙂

    #1775143
    Daniel

    Sorry Elvin I have just picked up on an issue.
    Website is now live at reimann.com.au
    When going to the first project post “Gawler East Link Road Project”, in the bottom post navigation it says “Previous project” which links to the same post.
    I need it to say “Next Project” and link to next post in Projects category menu order (not next ID).
    Thanks again
    Danny

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