Content only on last page, if the page has pagination

I want to show a donate button after the post and on the LAST page of paginated posts. While my code totally works, it is probably not the best way to do that. Has anybody a more elegant solution? Since the content (the donate button) is exactly the same, there is no need to call this HTML block twice. Thank you!

global $multipage, $numpages, $page;
if( $multipage && $page == $numpages ) { ?>
Content (on the last page of a paginated post)
<?php }  
if ($multipage == 0) { ?>
Content
<?php } ?>

1 Answer
1

Instead of using the global variables directly, here’s a way to use the content_pagination filter to add custom HTML to the last content page:

/**
 * Append HTML to the last content page, if it's paginated 
 */
add_filter( 'content_pagination', function( $pages )
{
    if( count( $pages ) > 1 )
        $pages[count($pages)-1] .= '<div>MY HTML HERE!</div>';

    return $pages;
} );

This appends the custom HTML to all last content pages, where there is content pagination.

Update: To include posts without content pagination, we can e.g. replace > 1 with > 0.

Leave a Comment