1

Is there a way to add a page break in WordPress programmatically?

I'm trying to do this within a PHP loop that goes inside a post as a widget, it generates information that it gets from YELP through the YELP API, but I want the loop to create a <!--nextpage--> page break each time it iterates through the loop. I have everything done except the part where I include the page break since it obviously doesn't work by just inserting that <!--nextpage--> tag since that gets executed before.

My question is, is there another way to do that? Perhaps a hook or filter or maybe a PHP equivalent? I'm not sure how else to clarify the question as I wasn't sure how to ask the question in the first place other than stating what I need.

Update: I've also tried using a plugin that allows me to use as a shortcode, I then tried to use do_shortcode within the PHP and it pops out the tag but taken in as a comment.

  • The Auto Read More Generator plugin's code shows how to add Read more link after the first <img> or the first </p> tag. You can go through this and try to put <!--nextpage--> there. Have a try. I never tried, so can't help. – Mayeenul Islam Jul 11 '15 at 6:30
1

Overriding the default page breaking - with external content parts

The page breaking takes place in the WP_Query::setup_postdata() method, when we call the_post() in the loop.

The page parts are stored in the global $pages array and fetched with get_the_content() function.

This happens all before the the_content filter is applied to the content.

We can modify this default page breaking, with our own get_the_content() wrapper.

Let's assume we got this in our theme template:

// Display
the_content();

// Paging
wp_link_pages();

Here below are two ways to modify the page breaking, using some external content parts.

1) Modification of a child theme

We replace this, in the corresponding child theme, with:

// Setup our content parts
$parts = [ get_the_content(), 'Part Two', 'Part Three', 'Part Four' ];

// Display
if( function_exists( 'get_the_content_part_wpse194155' ) )
    echo apply_filters( 'the_content', get_the_content_part_wpse194155( $parts ) );
else
    the_content();

// Paging
wp_link_pages();

2) Modification with a filter

We could also use the the_content filter. Example:

! is_admin() && add_filter( 'the_content', function( $content )
{
    if( ! in_the_loop() || ! function_exists( 'get_the_content_part_wpse194155' ) )
        return $content;

   // Setup our content parts
    $parts = [ $content, 'Part Two', 'Part Three', 'Part Four' ];

    // Display
    return get_the_content_part_wpse194155( $parts );

} );

Note that here we just use static content parts for demonstration. In general you would want to have different content parts for each post.

Helper function

Our custom wrapper is defined as:

/**
 * Modify globals to support page breaks between given content parts
 *
 * @param  array  $parts Array of content parts 
 * @return string|null   A single content part
 */

function get_the_content_part_wpse194155( array $parts )
{
    global $page, $pages, $multipage, $numpages;

    // No paging needed
    if( count( $parts ) <= 1 )
        return array_pop( $parts );

    // Init
    $out   = '';
    $break = '<!--nextpage-->';
    $page  = (int) get_query_var( 'page' );

    // Loop - add page breaks between parts
    foreach( (array) $parts as $part )
        $out .= $part . $break;

    // Remove the last empty page break
    if( count( $parts ) > 0 )
        $out = mb_substr( $out, 0, -1 * strlen( $break ) );

    // Adjust the globals
    $pages     = explode( $break, $out );
    $page      = ( $page > count( $pages ) || $page < 1 ) ? 1 : $page;
    $numpages  = count( $pages );
    $multipage = 1;    

    return $pages[$page-1];
}
| improve this answer | |
  • I can't figure out how to use this within my theme. I've been up all night trying to figure out how to make this thing work, I must be overlooking something. Trying to figure out how to make this work with the YELP API or the YELP pro premium plugin or anyway I can make the YELP results automatically break in to different pages within a post. – Lloan Alas Jul 11 '15 at 23:24
  • Since I don't know the YELP API, I just assumed the data could be transformed into an array of content parts. So if you got any array of strings, then you should be able to plug this in directly as described. @LloanAlas – birgire Jul 11 '15 at 23:31
  • What I'm doing is using a php file with the api functions & queries, using PHP Code within posts with a plugin - the only part I'm stuck at now is utilizing the response I get from the API and formatting it properly. I get a ton of stdClass objects within the JSON response. Feel a bit closer. – Lloan Alas Jul 12 '15 at 5:52
  • You mentioned a PHP loop, I guess you could construct the content array from there. If you got a string, then you could split it up into an array. As a last resort you could grab the output through output buffering and split it into an array. @LloanAlas – birgire Jul 12 '15 at 11:50

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.