A way to display a download PDF button at the bottom of every post created using ACF
P粉704066087
P粉704066087 2024-03-28 13:17:41
0
1
475

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粉704066087
P粉704066087

reply all(1)
P粉985686557

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;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template