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粉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; }