Home>Support>Plese check my approach to enablig Previous/Next to point only to posts within a category
  1. 9 years, 2 months ago Andrew Misplon
    Hi, I Work Here

    Hi Brian

    Thanks for reaching out. Unfortunately, doesn’t look like your code came through. To do this correctly you’ll need to use a child theme, here is a starter child theme:

    https://siteorigin.com/wp-content/uploads/2015/03/vantage-child-starter.zip

    Thanks to the if function exists statement we can drop the following function into the functions.php file in the child theme:

    if ( ! function_exists( 'vantage_content_nav' ) ) :
    /**
     * Display navigation to next/previous pages when applicable
     *
     * @since vantage 1.0
     */
    function vantage_content_nav( $nav_id ) {
    	if( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'infinite-scroll' ) ) {
    		return;
    	}
    	global $wp_query, $post;
    
    	// Don't print empty markup on single pages if there's nowhere to navigate.
    	if ( is_single() ) {
    		$previous = ( is_attachment() ) ? get_post( $post->post_parent ) : get_adjacent_post( false, '', true );
    		$next = get_adjacent_post( false, '', false );
    
    		if ( ! $next && ! $previous )
    			return;
    	}
    
    	// Don't print empty markup in archives if there's only one page.
    	if ( $wp_query->max_num_pages < 2 && ( is_home() || is_archive() || is_search() ) )
    		return;
    
    	// Add the shorten title filter
    	add_filter('the_title', 'vantage_content_nav_shorten_title');
    
    	$nav_class = 'site-navigation paging-navigation';
    	if ( is_single() )
    		$nav_class = 'site-navigation post-navigation';
    
    	?>
    	<nav role="navigation" id="<?php echo esc_attr( $nav_id ); ?>" class="<?php echo $nav_class; ?>">
    		<h1 class="assistive-text"><?php _e( 'Post navigation', 'vantage' ); ?></h1>
    
    	<?php if ( is_single() ) : // navigation links for single posts ?>
    
    		<div class="single-nav-wrapper">
    			<?php previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '←', 'Previous post link', 'vantage' ) . '</span> %title' ); ?>
    			<?php next_post_link( '<div class="nav-next">%link</div>', '%title <span class="meta-nav">' . _x( '→', 'Next post link', 'vantage' ) . '</span>' ); ?>
    		</div>
    
    	<?php elseif ( $wp_query->max_num_pages > 1 && ( is_home() || is_archive() || is_search() ) ) : // navigation links for home, archive, and search pages ?>
    
    		<?php vantage_pagination() ?>
    
    	<?php endif; ?>
    
    	</nav><!-- #<?php echo esc_html( $nav_id ); ?> -->
    	<?php
    
    	// Remove the shorten title filter
    	remove_filter('the_title', 'vantage_content_nav_shorten_title');
    }
    endif; // vantage_content_nav
    

    You can then edit the next previous strings as required. They are:

    	<?php previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '←', 'Previous post li', 'vantage' ) . '</span> %title' ); ?>
    	<?php next_post_link( '<div class="nav-next">%link</div>', '%title <span class="meta-nav">' . _x( '→', 'Next post link''vantage' ) . '</span>' ); ?>
    

    Hope that helps.

    Please, remember to not make edits directly to theme files in the parent theme. All WordPress themes completely overwrite their own folder during theme updates so it’s not safe to make changes in the parent.

Replies on this thread are closed. Please create a new thread if you have a question, or purchase a SiteOrigin Premium license if you need one-on-one email support.

Get The Most Out of SiteOrigin with SiteOrigin Premium

Find Out More