How to grab a specific page of content from paginated post?

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.

Leave a Comment