4

I want to totally disable this functionality from WordPress because I'm implementing my own version of this with Javascript.

When I try to grab all the content in a post via the get_the_content() or the_content() it gives me only the first page, since the content has the page breaks. When I use $post->the_content I get the full post but it's not formatted with HTML tags. I don't want to have to add those myself programatically.

So I either need to get all the content already formatted - the way to do this is unknown to me at the moment or disable wp_page_links() so it doesn't paginate my posts.

3 Answers 3

6

EDIT - Now that 4.4 is out, you should use the content_pagination filter. See birgire's answer below.


You can add formatting to raw post content by applying the content filters directly to $post->post_content:

echo apply_filters( 'the_content', $post->post_content );

This will bypass pagination by not using the get_the_content function, which is what the_content uses internally to retrieve the current page's content for multipage posts.

You can prevent wp_link_pages from outputting anything by applying a filter to its output before the function is called and using the __return_empty_string helper function:

add_filter( 'wp_link_pages', '__return_empty_string' );
1
  • Das it mane. That did the trick. You da man(or wo-man).
    – ninja08
    Commented Nov 16, 2015 at 4:03
8

New content pagination filter in WordPress 4.4

As of WordPress 4.4 we can use the content_pagination filter ( see ticket #9911 )

/**
 * Filter the "pages" derived from splitting the post content.
 *
 * "Pages" are determined by splitting the post content based on the presence
 * of `<!-- nextpage -->` tags.
 *
 * @since 4.4.0
 *
 * @param array   $pages Array of "pages" derived from the post content.
 *                       of `<!-- nextpage -->` tags..
 * @param WP_Post $post  Current post object.
 */
 $pages = apply_filters( 'content_pagination', $pages, $post );

This filter lives in the setup_postdata() method of the WP_Query class and will make it easier to modify the pagination pages.

Here are few examples how to remove the content pagination (PHP 5.4+):

Example #1

Here's how we can disable the content pagination:

/**
 * Disable content pagination
 *
 * @link http://wordpress.stackexchange.com/a/208784/26350
 */
add_filter( 'content_pagination', function( $pages )
{
    $pages = [ join( '', $pages ) ];
    return $pages;
} );

Example #2

If we want to only target the main query loop:

/**
 * Disable content pagination in the main loop
 *
 * @link http://wordpress.stackexchange.com/a/208784/26350
 */
add_filter( 'content_pagination', function( $pages )
{
    if ( in_the_loop() )
        $pages = [ join( '', $pages ) ];

    return $pages;
} );

Example #3

If we only want to target post post type in the main loop:

/**
 * Disable content pagination for post post type in the main loop
 *
 * @link http://wordpress.stackexchange.com/a/208784/26350
 */
add_filter( 'content_pagination', function( $pages, $post )
{
    if ( in_the_loop() && 'post' === $post->post_type )
        $pages = [ join( '', $pages ) ];

    return $pages;
}, 10, 2 );
2
  • Glad to know there is now a filter to make our lives easier :-) Commented Nov 16, 2015 at 10:45
  • absolutely, looks like the 4.4 release is scheduled early December @PieterGoosen
    – birgire
    Commented Nov 16, 2015 at 11:10
2

You can kill the "link pages" functionality in one filter by stripping the nextpage markers out of the post content:

function kill_pages($posts,$qry) {
 if ($qry->is_single()) {
  $posts[0]->post_content = preg_replace( '/<!--nextpage(.*?)?-->/', '',  $posts[0]->post_content );
 }
 return $posts;
}
add_filter('the_posts','kill_pages',1,2);

Your Answer

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

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