• Resolved nathanielchristopher

    (@nathanielchristopher)


    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]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Any place there’s a call to get_adjacent_post(), the first passed parameter should be true to link to the adjacent post with the same term assigned. If the same term you want used is not a category term, specify the desired taxonomy as a fourth parameter. The default is ‘category’, which is why your code doesn’t currently have a fourth parameter.
    https://developer.www.ads-software.com/reference/functions/get_adjacent_post/

    If your theme is no longer subject to periodic updates you can directly edit the theme code. Otherwise you can copy the function source code, including the if ( ! function_exists( 'new_standard_post_nav' ) ) : ... endif; parts, to a child theme or custom plugin to override the theme default.

    Thread Starter nathanielchristopher

    (@nathanielchristopher)

    Thank you very much. This is very helpful.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Restrict post-navigation to specific category’ is closed to new replies.