Site logo

Archived topic

Navigation for CPT and restricting posts to same category conflicting

5 replies · Started by Bandara on April 16, 2021

Viewing posts 1–6 of 6

I am trying to do two things (maybe 3)

1) restrict previous/next on posts to the same category
2) show previous/next on custom posts types

And the third which I haven't figured out at all

3) restricting CPT prev/next to specific taxonomy

To do #1, I added
add_filter( 'generate_category_post_navigation', '__return_true' );

That works.

However, when I do #2 using this code, it only works when I remove the code from #1 above


add_action( 'generate_after_entry_content', 'tu_cpt_footer_meta' );
function tu_cpt_footer_meta() {
    if ( is_singular( 'podcast' ) ) : ?>
        <footer class="entry-meta">
            <?php generate_entry_meta(); ?>
        </footer><!-- .entry-meta -->
    <?php endif;
}

How do I get these two bits of code to work at the same time?

And then the follow up is how do I achieve #3, for example restricting previous/next of the podcast CPT to the same taxonomy series?

Hi there,

For #2, you can actually tell GeneratePress that your CPT exists and that you want the footer meta to appear there: https://docs.generatepress.com/article/generate-footer-meta-post-types/

#3 is a bit more tricky, but possible with this filter:

add_filter( 'generate_post_navigation_args', function( $args ) {
    if ( is_singular( 'your-cpt' ) ) {
        $args['in_same_term'] = true;
        $args['taxonomy'] = 'your_taxonomy';
    }

    return $args;
} );

Hope this helps :)

Great! It all works now! Thank you.

So for number 3, I have added this:

/* Limits by custom post type taxonomy */
add_filter( 'generate_post_navigation_args', function( $args ) {
    if ( is_singular( 'podcast' ) ) {
        $args['in_same_term'] = true;
        $args['taxonomy'] = 'series';
    }
    return $args;
} );

What should I do if I have a second cpt with a different taxonomy? Can I just duplicate the whole thing replacing podcast and series?

No need to duplicate the entire thing, just add another is_singular() block:

/* Limits by custom post type taxonomy */
add_filter( 'generate_post_navigation_args', function( $args ) {
    if ( is_singular( 'podcast' ) ) {
        $args['in_same_term'] = true;
        $args['taxonomy'] = 'series';
    }

    if ( is_singular( 'series' ) ) {
        $args['in_same_term'] = true;
        $args['taxonomy'] = 'something-else';
    }

    return $args;
} );

Glad I could help!

Thanks!! I'm so glad I picked GP. You should be happy knowing that you are making so many good things possible in the world through your work.

Really appreciate that - thank you! :)

This archived topic is closed to new replies.