Home  >  Q&A  >  body text

A way to display a download PDF button at the bottom of every post created using ACF

I created a text field where I can add the url of the pdf file. Now I am trying to show it on every post after the content but it only shows the button and 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 )
        );
    }
}

P粉704066087P粉704066087228 days ago415

reply all(1)I'll reply

  • P粉985686557

    P粉9856865572024-03-29 00:15:13

    All you need to do is connect the button with existing content. With the current code it will be overwritten. Check the following 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( 'Download PDF',
                esc_url( $download_pdf )
            );
        }
        return $content;
    }

    reply
    0
  • Cancelreply