Search code examples
phpwordpresscustom-wordpress-pageswordpress-shortcode

How to show download pdf button in every post bottom created with ACF


I created the a text field in which I can add url of pdf file. Now I'm trying to show it on every post after the content but it only shows the button but not the content. If I remove the_content from add_action, it shows the content but not the download pdf button.

add_action( 'the_content', 'show_download_url' );
    
function show_download_url() {
    $download_pdf = get_post_meta( get_the_ID(), 'download_pdf', true );

    if ( $download_pdf ) {
        printf( '<a href="%s" class="btn btn-primary">Download PDF</a>',
            esc_url( $download_pdf )
        );
    }
}


Solution

  • All you need is to concat your button with existing content. With current code it will override. Check below code for reference:

    add_action( 'the_content', 'show_download_url' );
        
    function show_download_url($content) {
        $download_pdf = get_post_meta( get_the_ID(), 'download_pdf', true );
        if ( $download_pdf ) {
            $content .= sprintf( '<a href="%s" class="btn btn-primary">Download PDF</a>',
                esc_url( $download_pdf )
            );
        }
        return $content;
    }