0

In my case I've got to create articles where each article has a custom, unchanging "header" part far too rich for the title and the excerpt (it's to contain image, media etc), and then a few different pages of content. I've decided to do this using <!--nextpage--> to keep it lightweight and easy for whoever will create the pages - treat the first page as the unchanging header and attach page 2, 3, 4 under a fixed set of links - an custom menu generated in that category, replacing standard pagination "Page 1 2 3 4" links.

In order to do that, I'll have to call the_content() twice; once for page 1, and once for whichever page is chosen through the dedicated menu. Except I have no clue how to make the_content() extract n-th page, other than through appending /n/ to the URL.

Is there some variable, function, plugin, modification to the code that lets me extract a specific page of paginated post?

1

1 Answer 1

1

nextpage pagination is a bit odd. You will have to parse the post content. This should do it.

function get_nth_page($n=0,$content='') {
  if (empty($content)) {
    global $post;
    $content = $post->post_content;
  }
  if (empty($content)) {
    return false;
  }
  $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
  $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
  $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
  $pages = explode('<!--nextpage-->', $content);
  if (isset($pages[$n])) {
    return $pages[$n];
  }
}

You can pass $content explicitly or it will try to grab the global $post variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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