- This topic has 7 replies, 3 voices, and was last updated 4 months, 2 weeks ago by
Ying.
-
AuthorPosts
-
October 31, 2022 at 12:32 am #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,
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!
October 31, 2022 at 8:02 am #2395574David
StaffCustomer SupportIn the error code, it states Line 467 of your functions.php in your child theme.
Can you share what line of code that is ?Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/October 31, 2022 at 8:17 am #2395611José 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.
November 1, 2022 at 6:42 am #2396732David
StaffCustomer SupportApologies, 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.
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/November 1, 2022 at 12:28 pm #2397260José Antonio
Hi David, don’t worry, I understand.
Yeah, I use the block editor and work with GenerateBlocks Pro.
November 1, 2022 at 3:03 pm #2397403Ying
StaffCustomer SupportHi Jose,
In that case, you can build a post navigation using the
block 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 🙂
November 3, 2022 at 8:45 am #2400279José 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!
November 3, 2022 at 9:17 am #2400332Ying
StaffCustomer SupportYou are welcome!
We keep adding new features, it’s normal that you are not aware of it 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.