[Resolved] Display post navigation only for posts with a particular tag

Home Forums Support [Resolved] Display post navigation only for posts with a particular tag

Home Forums Support Display post navigation only for posts with a particular tag

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #2394696
    José Antonio

    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,

    Post navigation

    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.

    Post navigation error

    Could you help me with my code?

    Thanks in advance!

    #2395574
    David
    Staff
    Customer Support

    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 ?

    #2395611
    José Antonio

    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.

    #2396732
    David
    Staff
    Customer Support

    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.

    #2397260
    José Antonio

    Hi David, don’t worry, I understand.

    Yeah, I use the block editor and work with GenerateBlocks Pro.

    #2397403
    Ying
    Staff
    Customer Support

    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 🙂

    #2400279
    José Antonio

    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!

    #2400332
    Ying
    Staff
    Customer Support

    You are welcome!

    We keep adding new features, it’s normal that you are not aware of it 🙂

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