Skip to content

Instantly share code, notes, and snippets.

@mestrewp
Created May 28, 2012 18:28
Show Gist options
  • Save mestrewp/2820494 to your computer and use it in GitHub Desktop.
Save mestrewp/2820494 to your computer and use it in GitHub Desktop.
WordPress: Get prev/next post link
<?php
/*
* @param $prev_or_next string. Either 'prev' or 'next'
*
* @return string empty or HTML link to prev/next post
*/
function get_star_post_link( $prev_or_next ) {
$pon = $prev_or_next;
if ( ! in_array( $pon, array( 'prev', 'next' ) ) )
$pon = 'prev';
$text = array( 'prev' => '&larr; Previous Post', 'next' => 'Next Post &rarr;' );
$text = $text[ $pon ];
if ( 'next' == $pon )
$adj = get_next_post();
if ( 'prev' == $pon )
$adj = get_previous_post();
$link = '';
if ( ! is_null( $adj->ID ) ) {
$href = get_permalink( $adj->ID );
$title = get_the_title( $adj->ID );
$link = "<a href='$href' title='$title'>{$text}</a>";
}
return $link;
}
/*
Use like so:
$next = get_star_post_link('next');
$prev = get_star_post_link('prev');
echo '<div class="navigation">';
echo empty( $prev ) ? '' : "<div class='navigation-prev'>$prev</div>";
echo empty( $next ) ? '' : "<div class='navigation-next'>$next</div>";
echo '</div>';
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment