Skip to content
Advertisement

How to show previous_post_link () / next_post_link () in shortcode – WordPress

I have this shortcode (I wrote it in functions.php) that shows the read previous and read next links in a single post:

JavaScript

The problem I have is that when I add the shortcode:

JavaScript

when inspecting it shows the different structure and the links are outside their containers losing their styles:

enter image description here

I need them to be in the same structure as defined in my shortcode. How could I solve this? Please help.

Advertisement

Answer

This is because previous_post_link and next_post_link do not return a value, but write to the output buffer directly.

You need to use their counterparts that return the value, so that you can then concatenate those into your string – get_previous_post_link and get_next_post_link.

(If you check the source code for the former two functions, you’ll see that they are just wrapper functions that call the latter two, and echo their return values – https://developer.wordpress.org/reference/functions/previous_post_link/#source)

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement