WordPress.org

Forums

Twenty Thirteen
Nav-links (next/previous post links) to only link to specific categories (10 posts)

  1. demonboy
    Member
    Posted 1 year ago #

    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.

  2. nextart
    Member
    Posted 1 year ago #

    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

  3. demonboy
    Member
    Posted 1 year ago #

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

  4. nolaandy
    Member
    Posted 8 months ago #

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

  5. fjpoblam
    Member
    Posted 7 months ago #

    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…

  6. demonboy
    Member
    Posted 7 months ago #

    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; ?>
  7. fjpoblam
    Member
    Posted 7 months ago #

    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?

  8. demonboy
    Member
    Posted 7 months ago #

    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.

  9. fjpoblam
    Member
    Posted 7 months ago #

    @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.

  10. demonboy
    Member
    Posted 7 months ago #

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

Topic Closed

This topic has been closed to new replies.

About this Theme

About this Topic