3

Many plugins seem to be adding a filter/action hook to the_content to display related posts, advertisements and such. The problem is I these appear before the post pagination so it the pagination is pushed down below.

Is it possible to display the post pagination right after the content? It appears wp_link_pages can only be used inside the loop.

8

I guess you have:

the_content();
wp_link_pages();

in your theme file. So you can instead try the following (PHP 5.4+):

/**
 * Append the wp_link_pages to the content.
 */
! is_admin() && add_filter( 'the_content', function( $content )
{
    if( in_the_loop() ) 
    {
        $args = [ 'echo' => false ];        // <-- Adjust the arguments to your needs!
        $content .= wp_link_pages( $args );
    }
    return $content;
}, 10 );                                    // <-- Adjust the priority to your needs!

and then adjust the arguments and priority to your needs. Notice the echo parameter, it's set to false, because we need to return the output. You then have to remove wp_link_pages() from your (child) theme file.

Update:

If we don't want to remove the extra wp_link_pages() by hand, we can use the wp_link_pages filter to only display the output, within our the_content filter callback:

/**
 * Append the wp_link_pages to the content.
 */
! is_admin() && add_filter( 'the_content', function( $content )
{
    if( in_the_loop() ) 
    {
        $args = [ 'echo' => false, '_show' => true ];  // <-- Adjust the arguments to your needs!
        $content .= wp_link_pages( $args );
    }
    return $content;
}, 10 );                                              // <-- Adjust the priority to your needs!

/**
 * Only display wp_link_pages() output when the '_show' argument is true.
 */
add_filter( 'wp_link_pages', function( $output, $args )
{
    return ! isset( $args['_show'] ) || ! wp_validate_boolean( $args['_show'] ) ? '' : $output;
}, 10, 2 );

where we've introduced the extra _show argument for this purpose.

  • Wow, it works! Does that mean the hook binded to the_content is inside The Loop? – Jürgen Paul Apr 16 '15 at 9:25
  • 1
    great, you can add the in_the_loop() condition, check out this question by Pieter Goosen & the answer by gmazzap. @PineappleUndertheSea – birgire Apr 16 '15 at 9:37
  • 3
    it was a good question + answers, that helped many for sure ;-) @PieterGoosen – birgire Apr 16 '15 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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