Site logo

Archived topic

Next / Previous Post based within category

39 replies · Started by Michael on September 18, 2020

Viewing posts 1–15 of 40

Can't seem to find a solution by searching here in the forum, I have the next/previous links added, and styled as per instructions found here. The previous post in a category is showing as something that may be being fetched alphabetically from another category? Am terrible at trying to explain things.

Is there a way to control the next and previous so that it works within same categories only, and for custom post categories also? Would much rather do this within the theme than try some additional plugin.

Thanks as always.

Hi Leo, thank you, yes that was what I was looking for, however it doesn't work with custom post categories. How would I go about getting it to do that ?

Have posted a link to show where I mean, it's the WP Download Manager custom posts..

Are you saying that the post navigation doesn't show for that CPT or the category post navigation isn't working in CPT?

Post navigation not showing for that CPT, category post nav working well in standard post type.

Don't seem to be getting anywhere with that, I'm getting the meta output, but no navigation.

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

Is what I tried from the link you posted, replacing the custom post type name.

The code I'm using for the other navigation is..

add_filter( 'generate_post_navigation_args', function( $args ) {
    if ( is_single() ) {
        $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">' . $prevThumbnail . generate_get_svg_icon( 'arrow' ) . '<span class="prev" title="' . esc_attr__( 'Previous', 'generatepress' ) . '">%link</span></div>';
        $args['next_format'] = '<div class="nav-next">' . $nextThumbnail . generate_get_svg_icon( 'arrow' ) . '<span class="next" title="' . esc_attr__( 'Next', 'generatepress' ) . '">%link</span></div>';
    }

    return $args;
} );

Could I not modify the if is single line to include OR and the custom post type? It's a bit over my head the code structure, am just theorising.

I checked with download manager and that seems to be the way things are checked, from one of their forum posts

if ( is_singular( 'wpdmpro' ) ) {

Sorry where did the second function come from?

Can we try one at the time?

Did you make sure to clear the caching plugin after adding that first block of code?

The second function came from the these forums for post nav with thumbnails, they work. The first function came from me trying to add another function, to handle the CPT from the post you pointed me to, which just outputs the meta for that post.

I thought (hoped) that the first function could possibly be modified to check for the CPT, so that there was just one function.

I did clear the cache also, but still just delivers the meta.

Does the first function work without the second one?

The first works with or without the second, but it's just outputting tags no nav..

https://prnt.sc/ukm003

Ideally, for UX would like to keep the same layout across all.

Strange, is the custom post type using a custom taxonomy?

It's confusing, as far as I can tell it uses categories as the name for their taxonomy, but it's separate from the normal post categories taxonomies.

If it's not the core category taxonomy, you may need to do this:

add_filter( 'generate_post_navigation_args', function( $args ) {
    if ( is_singular( 'wpdmpro' ) ) {
        $args['taxonomy'] = 'the_taxonomy_slug';
    }

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