0

I'm missing something on how to call previous_post_link() inside of a function. I also tried assigning the output to a string variable and using that.

function prevnext( $content ) {
    $content .= '<div>' . previous_post_link() . '-' . next_post_link() . '</div>';
    return $content;
}
add_action( 'the_content', 'prevnext', 4 );

I get the hyphen - but not the links. Substituting another string variable also prints to the screen. I can use previous_post_link() in the page template and it works fine. What am I not doing right?

  • try this inside function and replace to get_previous_post_link() or get_next_post_link() – user5200704 Apr 28 '16 at 4:57
0

previous_post_link() and next_post_link() should work inside the the_content filter.

There is nothing wrong with your code except you must return the value inside the filter instead of print. Otherwise I believe post link should be there at the beginning of content.

previous_post_link() and next_post_link() print the output so consider using get_previous_post_link() and get_next_post_link().

function prevnext( $content ) {
    $content .= '<div>' . get_previous_post_link() . '-' . get_next_post_link() . '</div>';
    return $content;
}
add_action( 'the_content', 'prevnext', 4 );
  • 1
    Duh! Sometimes I amaze myself. I knew that. Thanks for pointing it out. That was all I needed to do - use get_ – mrmunk Apr 28 '16 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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