Adding an additional paginated page to the content

We know that adding <!–nextpage–> allows you to paginate the content of the post/page in WordPress.

Also if you want to append the content you can use the_content filter.

This filter though works but adds that content to all the pages instead of creating a new page for that content.

For example: I have a paginated post with 3 pages, separated <!–nextpage–>. I want to add an additional, fourth page, the_content will not allow that, so in that case, you can use the following code to add a new page to your paginated post.

add_filter( 'posts_results', 'change_the_posts_content' );
function change_the_posts_content( $posts ) {
  if ( is_array( $posts ) ) {
    foreach ( $posts as $post ) {
      if ( $post->post_type == 'post' ) {
        $post->post_content .= '<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-more="nextpage" class="wp-more-tag mce-wp-nextpage" alt="" title="Page break" data-mce-resize="false" data-mce-placeholder="1" / >test test test';
      }
    }
  }

  return $posts;
}Code language: PHP (php)

*You will need to change the post_type=”post” to post_type=”page” in order to get it to work with the pages.

Leave a comment

Your email address will not be published. Required fields are marked *