Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Let's say I have a post that has been paginated by adding the

 <!-- nextpage --> 

code provided by wordpress for this purpose. How can I then query a specific page from inside of that post. Say for instance I only want to query the contents of page 2 and there are 10 posts. Is there a way to do it?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

WordPress uses the PHP explode function to split the content into a array of 'pages'. Happens in the setup_postdata function with this code:

$pages = explode('', $content);

source

So you could do something like :

function wpse_103026( $content, $pagenum ) {
  if ( strpos( $content, '<!--nextpage-->' ) ) {
    $pages = explode('<!--nextpage-->', $content); 
    return isset ( $pages[$pagenum-1] ) ? trim( $pages[$pagenum-1] ) : $content;
    } else {
    return false;
  }
}

And then you can retrieve content of page 4 with :

echo apply_filters( 'the_content', wpse_103026( $post->post_content, 4 ) );
share|improve this answer
    
Fantastic! Thanks for the complete solution. –  MikeGWP Jun 14 '13 at 23:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.