• I wanted to have a separator between the next post and previous posts link. But when there is no next or previous post the separator would still show up, lame. get_adjacent_post doesn’t work as well as it should be, needs to get fixed! Anyways, here is how i figured it out below

    <?php
    $current =  get_permalink();
    $prevPost = get_previous_post(true);
    $prevURL = get_permalink($prevPost->ID);
    $nextPost = get_next_post(true);
    $nextURL = get_permalink($nextPost->ID);
    
    if ($current != $prevURL) {
    echo "<a href='$prevURL'>Previous</a>";
    }
    
    if ($current != $nextURL) {
    echo "<a href='$nextURL'>Next</a>";
    }
    ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter westondeboer

    (@poil11)

    <?php
    //get current posts permalink, need this for some reason
    $current =  get_permalink();
    $prevPost = get_previous_post(true);
    $prevURL = get_permalink($prevPost->ID);
    $nextPost = get_next_post(true);
    $nextURL = get_permalink($nextPost->ID);
    
    // checks to see if current posts isn't prevul if echo out image and link
    if ($current != $prevURL) {
    $NEXT = "<a href='$prevURL'><img src='/wp-content/themes/jesus/images/next.gif'></a>";
    } else {
    $prevURL = '';
    }
    
    // Same as above but for next post
    if ($current != $nextURL) {
    $PREVIOUS = "<a href='$nextURL'><img src='/wp-content/themes/jesus/images/previous.gif'></a>";
    } else {
    $nextURL = '';
    }
    
    // just echo em out if no seperator needed
    if (($prevURL == '') || ($nextURL == '')) {
    echo "$NEXT";
    echo "$PREVIOUS";
    
    // if no blanks put in a seperator also
    } else {
    echo "$PREVIOUS";
    echo "seprator";
    echo "$NEXT";
    }
    ?>

    ANyone help with an updated version of this that works better?

    Thread Starter westondeboer

    (@poil11)

    @poil11: I improved your code a bit and made a function out of it:

    function ynbs_customnav($current, $prevPost, $nextPost) {
    	$prevURL = get_permalink(get_previous_post(false)->ID);
    	$nextURL = get_permalink(get_next_post(false)->ID);;
    	$output = '';
    	// Print previous link if available
    	if ($current != $prevURL) {
    		$output .= '<a href="'.$prevURL.'" class="btn_previous btn left" hidefocus="hidefocus"><span><span><u class="ds">Previous&nbsp;post</u></span></span></a>'."\n";
    	}
    	// Print next link if available
    	if ($current != $nextURL) {
    		$output .= '<a href="'.$nextURL.'" class="btn_next btn right" hidefocus="hidefocus"><span><span><u class="ds">Next&nbsp;post</u></span></span></a>';
    	}
    	return $output;
    }
    
    print ynbs_customnav(get_permalink());

    You can now fully customize the HTML wrapping the links, maybe I’ll create a more generic function later for seperate use of Next and Previous links.
    Off course you could place the function inside functions.php. Like previous stated, this has to be used inside the loop!

    Blast, only just found out I forgot to take out the 2nd and 3rd argument, they became obsolete.

    Function should be like this:

    function ynbs_customnav($current) {
    	$prevURL = get_permalink(get_previous_post(false)->ID);
    	$nextURL = get_permalink(get_next_post(false)->ID);;
    	$output = '';
    	// Print previous link if available
    	if ($current != $prevURL) {
    		$output .= '<a href="'.$prevURL.'" class="btn_previous btn left" hidefocus="hidefocus"><span><span><u class="ds">Previous&nbsp;post</u></span></span></a>'."\n";
    	}
    	// Print next link if available
    	if ($current != $nextURL) {
    		$output .= '<a href="'.$nextURL.'" class="btn_next btn right" hidefocus="hidefocus"><span><span><u class="ds">Next&nbsp;post</u></span></span></a>';
    	}
    	return $output;
    }
    
    print ynbs_customnav(get_permalink());
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘HOWTO: Get link of previous and Next post in Category’ is closed to new replies.