2

I use wp_link_pages to paginate individual pages and posts.

Is there a way I can add to it an extra link which'll show all pages together, on a single page rather than split?

1

Here's a suggestion using the wp_link_pages and content_pagination filters:

The aim is to add a "Show All Content" link below the content pagination links:

enter image description here

and when we click it, the link should display "Show Content Pagination" and vice versa.

Demo plugin

We can do that with the following demo, where we introduce the wpse_show_all_content GET variable:

add_filter( 'wp_link_pages', function( $output, $args )
{
    // Validate user input
    $show_all_content = (bool) filter_input( 
        INPUT_GET, 
        'wpse_show_all_content', 
        FILTER_VALIDATE_BOOLEAN 
    ); 

    // Generate link
    $link = add_query_arg( 
        'wpse_show_all_content', 
        ! $show_all_content, 
        get_permalink() 
    );

    // Generate link title
    $title = $show_all_content 
        ? 'Show Content Pagination' 
        : 'Show All Content';

    // Append    
    $output .= sprintf( 
        '<a href="%s">%s</a>', 
        esc_url( $link ),
        esc_html__( $title, 'mydomain' )
    );

    return $output;

}, 10, 2 );

To disable the content pagination when wpse_show_all_content is true, we can use (PHP 5.4+):

add_filter( 'content_pagination', function( $pages )
{
    // Validate user input
    $show_all_content = (bool) filter_input( 
        INPUT_GET, 
        'wpse_show_all_content', 
        FILTER_VALIDATE_BOOLEAN 
    ); 

    // Disable content pagination
    if( $show_all_content )
        $pages = [ join( '', $pages ) ];

    return $pages;

} );

Hope you can adjust this further to your needs!

Update

As per discussion, here's a way to target only a specific wp_link_pages() instance:

add_filter( 'wp_link_pages', function( $output, $args )
{
    // Nothing to do if 'wpse_show_all_link' is missing or not a true boolean string
    if( 
            ! isset( $args['wpse_show_all_link'] ) 
        ||  ! wp_validate_boolean( $args['wpse_show_all_link'] )
    )
        return output;

    // Validate user input
    $show_all_content = (bool) filter_input( 
        INPUT_GET, 
        'wpse_show_all_content', 
        FILTER_VALIDATE_BOOLEAN 
    ); 

    // Generate link
    $link = add_query_arg( 
        'wpse_show_all_content', 
        ! $show_all_content, 
        get_permalink() 
    );

    // Generate link title
    $title = $show_all_content 
        ? 'Show Content Pagination' 
        : 'Show All Content';

    // Append    
    $output .= sprintf( 
        '<a href="%s">%s</a>', 
        esc_url( $link ),
        esc_html__( $title, 'mydomain' )
    );

    return $output;
}, 10, 2 );

where we introduce a custom wpse_show_all_link attribute that can take values like

1, 0, '1', '0', 'yes', 'no', true, false, 'true', 'false'.

Usage:

wp_link_pages( ['wpse_show_all_link' => true, ...] );
6
  • Thanks. That does it - although it's currently showing the link twice, and I don't know enough to fix that. – gulliver Sep 14 '16 at 11:00
  • Great. I tested this briefly on a vanilla install with the Twenty Sixteen theme so I suspect there's something affecting it in your theme or plugins. but it's difficult to say what that could be exactly Start small and try e.g. a plain link with the wp_link_pages filter and add from there. @gulliver – birgire Sep 14 '16 at 11:06
  • 1
    Sorry, my brain must have been freewheelin'. It shows twice because I'm using wp_link_pages twice - once to show a prev/next page and then again to show the page numbers. I'll find a way to change things. Your code works well. Thanks, again. ;-) (UPDATE... gist.github.com/drivenfaroff/7570106 has a function which combines numbers and prev/next.) – gulliver Sep 14 '16 at 11:22
  • 1
    You're welcome - good to hear you sorted it out @gulliver – birgire Sep 14 '16 at 11:24
  • Updated the answer with an example how to target a specific wp_link_pages() instance (untested though) @gulliver – birgire Sep 15 '16 at 15:38

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.