Site logo

Archived topic

Limit Post Navigation to Category

7 replies · Started by JANEK on July 15, 2018

Viewing posts 1–8 of 8

Good morning,

I just noticed that the post navigation that appears on single posts is not limited to the the posts category so will pick up everything. Is there a way to limit the post navigation to their respective category? Or will it be a case of just having to tell the post navigation to exclude certain categories I dont want displayed?

There's a filter for that!

add_filter( 'generate_category_post_navigation', '__return_true' );

That should do it :)

Awesome, is there a way to make this function only on the Post type. Rather than everything. I have a custom post type that I'd like to retain the original functionality on, but on my default Post type I just want it to navigate its respective categories.

You could try:

add_action( 'wp', 'tu_cpt_cat_specific_links' );
function tu_cpt_cat_specific_links() {
    if ( is_singular( 'your-post-type' ) ) {
        add_filter( 'generate_category_post_navigation', '__return_true' );
    }
}

Done and dusted! That was easy. Thank you! :)

You're welcome :)

Hey Tom,

Sorry to dredge this one up, it seems to not work on custom post types. For example I'm using the Seriously Simple Podcast plugin and its added a custom post type named 'podcast' to the site. However I dont think the podcast CPT uses the name categories which your code refers to but rather it uses a custom taxonomy which they've called 'series'. Would you be able to assist me in modifying your code to work with the new taxonomy and CPT name?

To give you an idea of what I'm trying to achieve... I have two series (categories) one is the main podcast series the other is titled next episode, and I want to keep the next episode out of the post navigation essentially. If that makes sense.

Your login is still valid for the site if you want to take a look at how the podcast series are setup.

The site url is:
http://staging.janek-dev.flywheelsites.com/

Yea, the taxonomy being different will mess it up.

I'm not 100% sure, but you could try this:

add_filter( 'get_next_post_join', 'tu_adjust_taxonomy_links' );
add_filter( 'get_previous_post_join', 'tu_adjust_taxonomy_links' );
function tu_adjust_taxonomy_links( $join, $in_same_term, $excluded_terms, $taxonomy ) {
    if ( ! is_singular( 'podcast' ) ) {
        return $join;
    }

    $taxonomy = 'series';

    return $join;
}
This archived topic is closed to new replies.