Archived topic
Display post navigation only for posts with a particular tag
7 replies · Started by José Antonio on October 31, 2022
Hi everyone! on my new website I use posts tagged with the "portfolio" term to create a simple portfolio.
I want display the post navigation only in these tagged posts, I use the solutions found in the forum, and it works fine,

This is my code:
// Add footer post navigation only for portfolio post.
add_filter( 'generate_post_navigation_args', function( $args ) {
if ( is_single() && has_tag('portfolio')) {
$prevPost = get_previous_post(true);
$prevThumbnail = isset($prevPost) ? get_the_post_thumbnail($prevPost->ID) : '';
$nextPost = get_next_post(true);
$nextThumbnail = isset($nextPost) ? get_the_post_thumbnail($nextPost->ID) : '';
$args['previous_format'] = '<div class="nav-previous"><a href="' . get_permalink($prevPost->ID) . '" rel="prev">' . $prevThumbnail . '<img class="arrow" alt="prev" src="' . get_stylesheet_directory_uri() . '/assets/media/icons/chevron-icon.svg"><span class="prev" title="' . esc_attr__('Previous', 'generatepress') . '">' . get_the_title($prevPost->ID) . '</a></span></div>';
$args['next_format'] = '<div class="nav-next"><a href="' . get_permalink($nextPost->ID) . '" rel="next"><span class="next" title="' . esc_attr__('Next', 'generatepress') . '">' . get_the_title($nextPost->ID) . '</span><img class="arrow" alt="next" src="' . get_stylesheet_directory_uri() . '/assets/media/icons/chevron-icon.svg">' . $nextThumbnail . '</a></div>';
}
return $args;
} );
and:
// Add footer code with customized post navigation.
add_action('generate_after_content', 'custom_footer_meta_portfolio');
function custom_footer_meta_portfolio() {
if (is_single() && has_tag('portfolio')) : ?>
<footer class="entry-meta">
<?php generate_content_nav('nav-below'); ?>
</footer><!-- .entry-meta -->
<?php endif;
}
But in the first and last post tagged with "portfolio" it doesn't work, and appears this error:
Notice: Trying to get property 'ID' of non-object in/Users/campoy.pro/macbook-pro-de-jose.local:5757/wp-content/themes/dev/functions.php on line 467.

Could you help me with my code?
Thanks in advance!
In the error code, it states Line 467 of your functions.php in your child theme.
Can you share what line of code that is ?
Hi David, first of all, thank you for your support.
The error refers to two lines:
467
$nextThumbnail = isset($nextPost) ? get_the_post_thumbnail($nextPost->ID) : '';
470
$args['next_format'] = '<div class="nav-next"><a href="' . get_permalink($nextPost->ID) . '" rel="next"><span class="next" title="' . esc_attr__('Next', 'generatepress') . '">' . get_the_title($nextPost->ID) . '</span><img class="arrow" alt="next" src="' . get_stylesheet_directory_uri() . '/assets/media/icons/chevron-icon.svg">' . $nextThumbnail . '</a></div>';
The post in which this error appears, is the most recent, there is no "next" post.
Apologies, your last reply slipped through our system.
Quick question - do you use the Block Editor ? As we could build this using GenerateBlocks and a GP Block Element.
Let me know. If not i think we may need to replace that function with a custom built post nav.
Hi David, don't worry, I understand.
Yeah, I use the block editor and work with GenerateBlocks Pro.
Hi Jose,
In that case, you can build a post navigation using theblock element - post navigation template:
https://docs.generatepress.com/article/block-element-post-navigation/
There's a tutorial video within the link, let us know if you run into any questions regarding building the block element :)
Hi Ying, it works perfectly!
Sorry for starting a topic without having studied the new features of GeneratePress and GenerateBlocks, it's a recommendation for everyone! 😅
I work for a long time with GeneratePress and I tend to be lazy...
Thank you all very much for your excellent support!
You are welcome!
We keep adding new features, it's normal that you are not aware of it :)