Created
May 28, 2012 18:28
-
-
Save mestrewp/2820494 to your computer and use it in GitHub Desktop.
WordPress: Get prev/next post link
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' => '← Previous Post', 'next' => 'Next Post →' ); | |
$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