Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I really need some help, I dont know where else to turn with this request. I'd like to add a shortcode to one of my posts. The template tags get_next_post() and get_previous will not work for me since I dont want post navigation on each post page and I want to control where it displays. In a DIV in an HTML structure I've created in a post.

So what I need is to generate shortcodes from get_next_post() and get_previous_post()

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

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....

share|improve this answer
    
Wow, thanks for getting back to me so quickly Zina. I've added this to my functions.php and I can call it in the post, with CSS I can make the .nav-previous visable on the frontend but for some reason I cant click/ navigate to the next post... Basically its a litle button like on of these > that should move to next/ previous post... –  brett Mar 24 '11 at 9:38
    
@brett -Hi,the reason you don't see the link is because those functions don't return a value but echo it. That's why you don't see it in the place you want.You have an option to edit the declarations in link-template.php by adding a new parameter in the end which indicates which action you want to do (return/echo). hope I helped you –  zina Mar 27 '11 at 9:42
    
Hi Zina, yep thank you –  brett Apr 20 '11 at 14:24
add comment

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;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.