Wordpress - Making a Shortcode [NEXT] and [PREVIOUS] to place into specific posts for post navigation
this is very simple to do...
// next
function next_shortcode($atts) {
// global $post; -unnecessary
return '<div class="nav-next">'.next_post_link( '%link', '%title <span class="meta-nav">' . _x( '', 'Next post link', ' ' ) . '</span>',true ).'</div>';
}
add_shortcode( 'next', 'next_shortcode' );
//prev
function prev_shortcode($atts) {
//global $post; -unnecessary
return '<div class="nav-previous">'.next_post_link( '%link', '%title <span class="meta-nav">' . _x( '', 'Previous post link', ' ' ) . '</span>',true ).'</div>';
}
add_shortcode( 'prev', 'prev_shortcode' );
Goodluck! Here for any question....
There were a couple issues with the above code. You need to wrap shortcode output with ob_start() in order to get it to echo to the screen in the right place. Also, as one of the commenters above mentioned, those functions already echo their result so putting them in a return statement won't work correctly.
Here is my fix:
add_shortcode( 'prev', 'prev_shortcode' );
add_shortcode( 'next', 'next_shortcode' );
function next_shortcode($atts) {
global $post;
ob_start();
next_post_link( '<div class="nav-next">%link</div>', 'Next post link' );
$result = ob_get_contents();
ob_end_clean();
return $result;
}
function prev_shortcode($atts) {
global $post;
ob_start();
previous_post_link( '<div class="nav-previous">%link</div>', 'Previous post link' );
$result = ob_get_contents();
ob_end_clean();
return $result;
}