Skip to content

Instantly share code, notes, and snippets.

@carlosonweb
Created September 30, 2017 07:29
Show Gist options
  • Save carlosonweb/843f2bdf9fffd125d8a4f21e54abc39c to your computer and use it in GitHub Desktop.
Save carlosonweb/843f2bdf9fffd125d8a4f21e54abc39c to your computer and use it in GitHub Desktop.
Display Previous Next Navigation Links via Shortcode
//
// For simplicity, the code below assumes that the CPT is 'project'
//
// TODO: Improve further.
// ------------------------------------------------
add_shortcode('project-navigation', function(){
$html = '';
$prev_post_link = '';
$next_post_link = '';
if( is_singular('project') ) {
$prev_post = get_previous_post();
if ( $prev_post ){
$prev_post_link = '<a href="' . get_permalink( $prev_post ) . '">'. $prev_post->post_title . '</a>';
}
$next_post = get_next_post();
if ( $next_post ){
$next_post_link = '<a href="' . get_permalink( $next_post ) . '">'. $next_post->post_title . '</a>';
}
$html .= '<div class="post-nav">';
$html .= '<div class="alignleft prev-next-post-nav">' . $prev_post_link . '</div>';
$html .= '<div class="alignright prev-next-post-nav">' . $next_post_link . '</div>';
$html .= '</div>';
}
return $html;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment