• I have different categories and I’d like my nav-links (next post / previous post) at the bottom of the page to only link within specific categories. For example, when I’m in the category ‘blog’, the next and previous links only link to the next or previous post that falls within the blog category. At the moment, they just link to the next or previous post whichever the category.

    The problem is that the nav-links are set in the functions.php file, not in any of the templates, so I’m assuming I have to create a whole load of if statements within function.php to call up each category… or something.

    Can anyone help? The relevant code is here:

    function twentythirteen_paging_nav() {
    	global $wp_query;
    
    	// Don't print empty markup if there's only one page.
    	if ( $wp_query->max_num_pages < 2 )
    		return;
    	?>
    	<nav class="navigation paging-navigation" role="navigation">
    		<h1 class="screen-reader-text"><?php _e( 'Posts navigation', 'twentythirteen' ); ?></h1>
    		<div class="nav-links">
    
    			<?php if ( get_next_posts_link() ) : ?>
    			<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentythirteen' ) ); ?></div>
    			<?php endif; ?>
    
    			<?php if ( get_previous_posts_link() ) : ?>
    			<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentythirteen' ) ); ?></div>
    			<?php endif; ?>
    
    		</div><!-- .nav-links -->
    	</nav><!-- .navigation -->
    	<?php
    }
    endif;
    
    if ( ! function_exists( 'twentythirteen_post_nav' ) ) :
    /**
     * Displays navigation to next/previous post when applicable.
    *
    * @since Twenty Thirteen 1.0
    *
    * @return void
    */
    function twentythirteen_post_nav() {
    	global $post;
    
    	// Don't print empty markup if there's nowhere to navigate.
    	$previous = ( is_attachment() ) ? get_post( $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', 'twentythirteen' ); ?></h1>
    		<div class="nav-links">
    
    			<?php previous_post_link( '%link', _x( '<span class="meta-nav">&larr;</span> %title', 'Previous post link', 'twentythirteen' ) ); ?>
    			<?php next_post_link( '%link', _x( '%title <span class="meta-nav">&rarr;</span>', 'Next post link', 'twentythirteen' ) ); ?>
    
    		</div><!-- .nav-links -->
    	</nav><!-- .navigation -->
    	<?php
    }
    endif;

    Thanks in advance.

Viewing 9 replies - 1 through 9 (of 9 total)
  • hi try this:
    Text As Link, Without Post Title, Within Same Category

    Displays custom text as link to the next post within the same category as the current post. Post title is not included here. “Next post in category” is the custom text, which can be changed to fit your requirements.

    <?php previous_post_link('%link', 'Previous post in category', TRUE); ?>
    <?php next_post_link('%link', 'Next post in category', TRUE); ?>

    as codex say
    here

    bye
    Maurizio

    Thread Starter demonboy

    (@demonboy)

    Thanks, Maurizio. I’m not at my pc at the moment but I’ll try this when I return. Cheers.

    Unfortunately I have the same problem and the above solution did not work. Did you ever find a solution?

    Superb hint. Working for me. I was looking for a way to add a “Home” link on all but the “Home” page, so the “if get_previous…” is ideal. The only conceptual case in which it *doesn’t* is, when the visitor is looping newer and newer through a category set and reaches the newest, that being *not* the home page. I’ll have to think about it…

    Thread Starter demonboy

    (@demonboy)

    OK, the complete code is taken from my loop-nav.php, which is obviously specific to my theme, but here it is. Hope it helps:

    <?php if ( is_attachment() ) : ?>
    
    		<div class="loop-nav">
    			<?php previous_post_link( '%link', '<span class="previous">' . __( '<span class="meta-nav">&larr;</span> Return to entry', 'chun' ) . '</span>' ); ?>
    		</div><!-- .loop-nav -->
    
    	<?php elseif ( is_singular( 'post' ) && in_category('industry-interviews') )  : ?>
    
    		<div class="loop-nav">
    			<?php previous_post_link( '%link', '<span class="previous">' . __( '<span class="meta-nav">&larr;</span> Previous Interview', 'chun' ) . '</span>', TRUE ); ?>
    			<?php next_post_link( '%link', '<span class="next">' . __( 'Next Interview<span class="meta-nav">&rarr;</span>', 'chun' ) . '</span>', TRUE ); ?>
    		</div><!-- .loop-nav -->
    
    	<?php elseif ( is_singular( 'post' ) && in_category('stories') )  : ?>
    
    		<div class="loop-nav">
    			<?php previous_post_link( '%link', '<span class="previous">' . __( '<span class="meta-nav">&larr;</span> Previous Story', 'chun' ) . '</span>', TRUE ); ?>
    			<?php next_post_link( '%link', '<span class="next">' . __( 'Next Story<span class="meta-nav">&rarr;</span>', 'chun' ) . '</span>', TRUE ); ?>
    		</div><!-- .loop-nav -->
    
    	<?php elseif ( is_singular( 'post' ) && in_category('lizs-competition-entries') )  : ?>
    
    		<div class="loop-nav">
    			<?php previous_post_link( '%link', '<span class="previous">' . __( '<span class="meta-nav">&larr;</span> Previous Competition Entry', 'chun' ) . '</span>', TRUE ); ?>
    			<?php next_post_link( '%link', '<span class="next">' . __( 'Next Competition Entry<span class="meta-nav">&rarr;</span>', 'chun' ) . '</span>', TRUE ); ?>
    		</div><!-- .loop-nav -->
    
    	<?php elseif ( is_singular( 'post' ) && in_category('blog') )  : ?>
    
    		<div class="loop-nav">
    			<?php previous_post_link( '%link', '<span class="previous">' . __( '<span class="meta-nav">&larr;</span> Previous blog post', 'chun' ) . '</span>', TRUE ); ?>
    			<?php next_post_link( '%link', '<span class="next">' . __( 'Next blog post<span class="meta-nav">&rarr;</span>', 'chun' ) . '</span>', TRUE ); ?>
    		</div><!-- .loop-nav -->
    
    	<?php elseif ( is_singular( 'post' ) && in_category('competitions') )  : ?>
    
    		<div class="loop-nav">
    			<?php previous_post_link( '%link', '<span class="previous">' . __( '<span class="meta-nav">&larr;</span> Previous competition', 'chun' ) . '</span>', TRUE ); ?>
    			<?php next_post_link( '%link', '<span class="next">' . __( 'Next competition<span class="meta-nav">&rarr;</span>', 'chun' ) . '</span>', TRUE ); ?>
    		</div><!-- .loop-nav -->
    
    	<?php elseif ( is_singular( 'post' ) ) : ?>
    
    		<div class="loop-nav">
    			<?php previous_post_link( '%link', '<span class="previous">' . __( '<span class="meta-nav">&larr;</span> Previous', 'chun' ) . '</span>', TRUE ); ?>
    			<?php next_post_link( '%link', '<span class="next">' . __( 'Next <span class="meta-nav">&rarr;</span>', 'chun' ) . '</span>', TRUE ); ?>
    		</div><!-- .loop-nav -->
    
    	<?php elseif ( !is_singular() && current_theme_supports( 'loop-pagination' ) ) : loop_pagination( array( 'prev_text' => __( '<span class="meta-nav">&larr;</span> Previous', 'chun' ), 'next_text' => __( 'Next <span class="meta-nav">&rarr;</span>', 'chun' ) ) ); ?>
    
    	<?php elseif ( !is_singular() && $nav = get_posts_nav_link( array( 'sep' => '', 'prelabel' => '<span class="previous">' . __( '<span class="meta-nav">&larr;</span> Previous', 'chun' ) . '</span>', 'nxtlabel' => '<span class="next">' . __( 'Next <span class="meta-nav">&rarr;</span>', 'chun' ) . '</span>' ) ) ) : ?>
    
    		<div class="loop-nav">
    			<?php echo $nav; ?>
    		</div><!-- .loop-nav -->
    
    	<?php endif; ?>

    Wow, @demonboy, intense!. That’s a lotta divs and spans plopped into your code. I reckon you’re popping them by name as targets in your external either css or js, no?

    Thread Starter demonboy

    (@demonboy)

    Well you could put the

    <div class="loop-nav">

    and

    </div><!-- .loop-nav -->

    outside of the if statement to save a few lines of code.

    @demonboy – Oh, I humbly apologize if you took my comment as offensive or critical. Not intended that way, at all. Your code is wonderfully legible and shows an admirable knowledge of both PHP and the WordPress call-outs: I wish I had WordPress memorized as well! I will take your post under advisement. My current template is classless, id-less, divless, and spanless… You may deduce that it is an entirely simple-minded matter—inspired by Jeff Starr’s H5. I will consider either popping in your example verbatim or butchering it with love to fit my playground.

    Thread Starter demonboy

    (@demonboy)

    No, you were right. That was bad coding on my behalf. Only need for one div, no point repeating unnecessary code.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Nav-links (next/previous post links) to only link to specific categories’ is closed to new replies.