Skip Navigation

[Resolved] Echo pagination in custom shortcode

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to use a custom shortcode to display some custom pagination links, but when I try to echo the code in PHP it is displayed in the wrong place on the page.

Solution: Check the documentation for the Shortcode API. It's best practice to build a string with concatenation, then return that string. Echo should not be used in a shortcode.

Relevant Documentation:
https://codex.wordpress.org/Shortcode_API

This support ticket is created 6 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 8.83 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by Tiago Valente 6 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1096299
Sem Título.png

I am trying to: place previous and next CPT inside single view.

Link to a page where the issue can be seen:

I expected to see: the previous and next post buttons on the place that was defined.

Instead, I got: previous and next post buttons on the start of the page, just below the logo.

This is the code inside functions.php:

function pagination() {
    echo '<div class="pagination-cpt"';
	echo '<div class="previous-post">';
	previous_post_link();
		echo '</div>';
	 echo '<div class="next-post">';
	next_post_link();
		echo '</div>';
	echo '</div>';
}
add_shortcode( 'pagination', 'pagination' );

I placed the [pagination] shortcode inside the Toolset layouts cell (which is at the bottom of the page).

See the screenshot.
It places the previous and next buttons at the start of the page, just below the logo. Thew the HTML you can see that it's placed at the beginning of the page instead going inside the container.

How can i solve this issue?

thanks

#1096369

Hi, check the documentation for WordPress Shortcodes: https://codex.wordpress.org/Shortcode_API

You shouldn't call echo in a shortcode function. Instead, you should build a string by concatenation, then return that string. Something like this:

function pagination_function() {
  $pg  = '';
  $pg .= '<div class="pagination-cpt">';
  $pg .= '<div class="previous-post">';
  $pg .= get_previous_post_link();
  $pg .= '</div>';
  $pg .= '<div class="next-post">';
  $pg .= get_next_post_link();
  $pg .= '</div>';
  $pg .= '</div>';
  
  return $pg;
}
add_shortcode( 'pagination', 'pagination_function' );
#1096854

Always learning.
Thanks Christian, that worked like a charm!