How Can I Query a Specific Page From a MultiPage paginated Post

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?

Related posts

1 comment

  1. 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 ) );
    

Comments are closed.