• Am i missing some key understanding in how to make changes to some of the built in wordpress functions. I wanted to restrict the previous and next links on my site to only link to posts in the same category.

    The basic function looks like this:
    <?php the_post_navigation(); ?>

    If I split this into two functions I can set an option to TRUE so that the nav links only to same category:

    <?php previous_post_link('%link', '%title', TRUE); ?>
    <?php next_post_link( '%link', '%title', TRUE ); ?>

    But when I do this I lose all the markup code around around the links, so at the end I have to replace all the divs and I get this:

    <nav class="navigation post-navigation" role="navigation">
    <h2 class="screen-reader-text">Post navigation</h2>
    			<div class="nav-links">
    			<div class="nav-previous">
    			<?php previous_post_link('%link', '%title', TRUE); ?>
    			</div>
    			<div class="nav-next">
    			<?php next_post_link( '%link', '%title', TRUE ); ?>
    			</div>
    				</div>
    			</nav>

    This seems like a pain in the backside to have to do everytime I want to make a small change – surely I am missing some simpler way of editing the original function? Note: I am using _s theme.

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

    (@jcastaneda)

    THEME COFFEE MONKEY

    This will work only for 4.1 and up:

    function custom_nav(){
    	$navigation = '';
    	$previous   = get_previous_post_link( '<div class="nav-previous">%link</div>', '%title', true );
    	$next       = get_next_post_link( '<div class="nav-next">%link</div>', '%title', true );
    
    	// Only add markup if there's somewhere to navigate to.
    	if ( $previous || $next ) {
    		$navigation = _navigation_markup( $previous . $next, 'post-navigation' );
    	}
    
    	echo $navigation;
    }

    Reason is that _navigation_markup was introduced in 4.1.

    Thread Starter danskkr

    (@danskkr)

    Lovely jubbly – thank you for the markup! Its all starting to make sense now 🙂

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Editing functions: the_post_navigation();’ is closed to new replies.