Restrict post-navigation to specific category
-
I have two main categories on my website: Blog (1823) and News (1831) and I would like to restrict post navigation within those categories.
In other words, if I’m looking at a post within the News category I would like to restrict the post navigation to other posts in the News category. Same with the posts in the Blog category.
At present the post navigation links point to the previous and next posts irrespective of their category. Consequently, the navigation menu within my Blog category may point to posts in the News category and vice-versa. I would like to avoid this.
I’m using the theme New Standard which is no longer supported by its author.Here’s where the post navigation code lives in “content-single.php”:
<?php if ( ! get_theme_mod( 'new_standard_hide_post_nav' ) ) : ?> <?php new_standard_post_nav(); ?><?php endif; ?><?php if ( is_active_sidebar( 'after-post' ) ) : ?> <?php dynamic_sidebar( 'after-post' ); ?><?php endif; ?>
And here’s where that lives in the theme’s function files:
if ( ! function_exists( 'new_standard_post_nav' ) ) : /** * Display navigation to next/previous post when applicable. */ function new_standard_post_nav() { // Don't print empty markup if there's nowhere to navigate. $previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true ); $next = get_adjacent_post( false, '', false ); if ( ! $next && ! $previous ) { return; } ?> <nav class="navigation post-navigation" role="navigation"> <h1 class="screen-reader-text"><?php _e( 'Post navigation', 'new-standard' ); ?></h1> <div class="nav-links"> <?php previous_post_link( '<div class="nav-previous"><div class="post-nav-title">' . __( 'Older post', 'new-standard' ) . '</div>%link</div>', _x( '%title', 'Previous post link', 'new-standard' ) ); next_post_link( '<div class="nav-next"><div class="post-nav-title">' . __( 'Newer post', 'new-standard' ) . '</div>%link</div>', _x( '%title', 'Next post link', 'new-standard' ) ); ?> </div><!-- .nav-links --> </nav><!-- .navigation --> <?php } endif;
I’m a bit of a novice with PHP so I appreciate any guidance or help.
The page I need help with: [log in to see the link]
- The topic ‘Restrict post-navigation to specific category’ is closed to new replies.