Archived topic
Limit custom post navigation to current taxonomy
8 replies · Started by ic7 on October 18, 2019
Hi,
I'm using GeneratePress & GP Premium.
I would like to limit the post navigation of a custom post type to the current posts custom taxonomy.
Is that possible?
Thanks for your help in advance!
Best regards,
Sebastian
Hi there,
Give this a shot:
add_filter( 'generate_category_post_navigation', '__return_true' );
Let me know :)
Hi Tom,
I should have mentioned, I'm using this code to generate the custom post type navigation:
add_action( 'generate_after_entry_content', 'tu_cpt_footer_meta' );
function tu_cpt_footer_meta()
{
if ( 'post' == get_post_type() || 'projekte' == get_post_type() ) : ?>
<footer class="entry-meta">
<?php generate_entry_meta(); ?>
<?php if ( is_single() ) generate_content_nav( 'nav-below' ); ?>
</footer><!-- .entry-meta -->
<?php endif;
}
The above filter results in no output.
It seems like we need to add another parameter here to include the taxonomy: https://github.com/tomusborne/generatepress/blob/2.3.2/inc/structure/post-meta.php#L53
This has come up a couple of times, so I'm going to add a new filter in GP 2.4. I'll update this topic once I commit the change so you can make the change on your installation :)
Hi there,
I've added a new filter in GP 2.4: https://github.com/tomusborne/generatepress/commit/4b1c9275f87fb340144dd4ae3037848fcc9c93a8
This will allow you to do this:
add_filter( 'generate_post_navigation_args', function( $args ) {
$args['taxonomy'] = 'your-custom-taxonomy';
return $args;
} );
Hi Tom,
thanks for adding this new filter to the new version!
I couldn't get the desired output with the 2.4-beta1, but I'm sure I missed something.
The custom post type is called 'projekte', with a custom taxonomy called 'bereiche'.
This taxonomy has different terms added, let's call them 'term-1', 'term-2', 'term-3', ...
Now if a user views a single post added to 'term-1', I want the post navigation to only display links to posts added the same term.
Is this possible with the new filter?
Here's the code I'm currently working with, but it's missing the term limitation:
add_action( 'generate_after_entry_content', 'tu_cpt_footer_meta' );
function tu_cpt_footer_meta()
{
if ( 'post' == get_post_type() || 'projekte' == get_post_type() ) : ?>
<footer class="entry-meta">
<?php generate_entry_meta(); ?>
<?php if ( is_single() ) generate_content_nav( 'nav-below' ); ?>
</footer><!-- .entry-meta -->
<?php endif;
}
add_filter( 'generate_post_navigation_args', function( $args ) {
$args['taxonomy'] = 'bereiche';
return $args;
} );
Thanks for your help in advance!
Hi there,
Try this:
add_filter( 'generate_post_navigation_args', function( $args ) {
if ( 'projekte' == get_post_type() ) {
$args['in_same_term'] = true;
$args['taxonomy'] = 'bereiche';
}
return $args;
} );
Let me know :)
This works perfectly, Tom!
Thank you very much!
Awesome! No problem :)