2

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

2

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.

3
  • Thank you very much, but that wouldn't do the trick. As stated in my question, I need my HTML content on ALL posts (but only on the last page of paginated posts). Your code would skip all posts with no pagination. It works perfect on paginated posts though. Jul 22, 2016 at 10:24
  • There are various ways to include posts without content pagination, I updated the answer with one suggestion @user2516117
    – birgire
    Jul 22, 2016 at 10:31
  • 1
    Oh my, of course! Excellent. Works like a charm! Jul 22, 2016 at 14:34

Your Answer

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

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