Archived topic
Display post navigation to show PREVIOUS and NEXT for particular category
25 replies · Started by Daniel on April 27, 2021
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!
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.
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?
Hi there,
Can you link us to a sample page in question? So we could inspect the links for any issues.
Sure, here is page with NEXT and PREVIOUS links;
https://staging2.reimann.com.au/projects/coomandook-tank-roof-and-mscl
Here is page with post title links;
https://staging2.reimann.com.au/services/specialty-design-and-fabrication-projects
Thanks
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.
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.
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' );
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?
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.
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 >
Can you try adding this filter? so the post stays within its category
add_filter('generate_category_post_navigation','__return_true');
Thanks Elvin works great now with navigation staying within same category.
I appreciate your fantastic support.
No problem. Glad you got it sorted. :)
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