1

Anyone implemented this successfully? I want to include the <!--nextpage--> within a single post loop as a shortcode. I tried to echo it, the tag shows up but the post doesn't get divided into pages.

I need to able to echo do_shortcode('[nextpage]'); within the single.php and actually divide it into different pages...

2
  • The closest you are going to come here is using the approach by @birgire. I don't think what you are trying to do will work because you cannot split the_content() in such a way Apr 12, 2015 at 14:27
  • have you find the method @DavidReis ? cause im looking for it too
    – fahmial
    Feb 13, 2017 at 9:10

1 Answer 1

2

Using the the_posts filter:

Here's one idea using the the_posts filter, that fires before setup_postdata() is activated:

/**
 * Replace [nextpage] with <!--nextpage--> through the 'the_posts' filter.
 *
 * @see http://wordpress.stackexchange.com/a/183980/26350
 */

! is_admin() && add_filter( 'the_posts', function( $posts )
{
    $posts = array_map( function( $p )
    {
        if ( false !== strpos( $p->post_content, '[nextpage]' ) )
            $p->post_content = str_replace( '[nextpage]', '<!--nextpage-->', $p->post_content ); 
        return $p;
    }, $posts );
    return $posts;
});

The reason why your approach doesn't work, is that your [nextpage] shortcode is generated after the setup_postdata(), that handles the post pagination, has checked for <!--nextpage--> in the post content.

Example:

Here I test it with some WordPress Lorem Ipsum:

back-end

with the following output on the front-end of the TwentyFifteen default theme:

front-end

8
  • Thanks @birgire should i copy that to my functions.php and then just do_shortcode [nextpage] on my single template?
    – David Reis
    Apr 12, 2015 at 11:21
  • didnt work for me...
    – David Reis
    Apr 12, 2015 at 11:33
  • try to add this code snippet into the functions.php or better yet as a plugin. This approach doesn't need you to define [nextpage] as a shortcode. So try to add [nextpage] in the post content and see what happens on the front-end.
    – birgire
    Apr 12, 2015 at 11:36
  • @DavidReis did some testing, seems to work as expected
    – birgire
    Apr 12, 2015 at 11:55
  • hmm still doesnt work.
    – David Reis
    Apr 12, 2015 at 11:58

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.