Using <!--nextpage--> within a shortcode

Here's one issue I am currently trying to solve and it concerns using !--nextpage-- within a shortcode.

I'm developing a plugin using a shortcode (unfortunately I don't have any URL to share since I'm currently developing this locally using a MAMP environment). Depending on the parameters used when calling this shortcode, it will display different kind of products in the post.

Since there can potentially be a large number of products to display, I'd like to paginate the post so that visitors don't have a huge vertical scroll. Basically, what I'm trying to achieve is the same kind of display that we see on any online store where you have a fixed number of products per page when browsing one category of products. On Amazon for example, you have around 20 articles per page: https://www.amazon.fr/s?bbn=342808031rh=n%3A342808031%2Cp_n_date%3A183218031dcfst=as%3Aoffqid=1564139237rnid=183215031ref=lp_342808031_nr_p_n_date_0

Wordpress does have a nice tag called !--nextpage-- to do so. This tag works fine when you're writing a post but after doing some research, it appears that using the !--nextpage-- tag within a shortcode will do nothing but add an html comment since when rendering a post, this tag is handled by a Wordpress function called "setup_postdata" before parsing the shortcode.

Going on with my research and tests, I found a lot of online discussions where people are talking about adding a filter or hooking inside the post but none of the solutions that I've tried so far have worked as it appears that the "setup_postdata" rendering function is always called prior to any other action inside the loop. See for example this answer inside this topic: https://stackoverflow.com/questions/17900836/adding-nextpage-dynamically-to-wordpress-post#comment26738787_17900836

I'll put some context to my issue.

  • Why using shortcode?

Because I'd like the writers on the website (who are not technical guys at all) to be able to put the products they want at the end of their post. So it'll be very easy for them to understand how to put a shortcode with the proper parameters. The idea is to basically be able to display any kind of products on any kind of post / page they decide to.

  • Am I using WP Query?

No, I'm using the standard wpdb class to request the specific tables I've added in the Wordpress database.

  • Loop / Template?

I did not create any specific loop or post template, I'm using the default ones provided by Wordpress.

  • List of posts/products?

I'm trying to display a list of products inside a single post by slipping one post like you would normally do when writing a very long article (like it says here: https://en.support.wordpress.com/nextpage/view-all/). And since, inside my shortcode, I am (basically) doing a foreach loop with my list of products (like it says here: https://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results), I'd like to put a !--nextpage-- tag every N products (N being 20, 30, etc. whatever number I choose to).

In a very simplified way, I'm trying to do this:

function my_shortcode_function ( $atts )
{
$variable = null;
$fivesdrafts = $wpdb-get_results(...);

foreach ( $fivesdrafts as $fivesdraft ) 
{
    $variable .= first product;
    $variable .= second product;
    etc.

    if (productNumber == 10 or productNumber == 20 or etc.) {
        $variable .= '!—nextpage--';
    }
    return $variable;
}
add_shortcode( ‘my_shortcode_name’, 'my_shortcode_function' );

So my question is:

Is there a way to use the !--nextpage-- tag when developing your own shortcode? One small precision: I do know how to develop a little in LAMP environment but I'm far from being an expert neither in programming nor in Wordpress programming. Anyway, any help would be greatly appreciated.

Thanks a lot.

Topic nextpage shortcode Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.