PHP: Check link content and display appropriate text on button

I’m creating a site in WordPress where some PDFs will be uploaded for users to download. Now I’m struggling a bit with PHP to figure out a way to display two different texts on the download button, depending on wether a PDF has been uploaded yet or not.

The button is currently made this way:

Read More
<a href="<?php the_field('presentation_pdf'); ?>" target="_new" class="small round button">Download</a>

What I need help to figure out now is a php-function where it checks if the link is to a PDF-file or not, and display the texts: Download (Link leads to a PDF-file) or “Not yet uploaded” if the link is “empty”.

Related posts

Leave a Reply

1 comment

  1. assuming your pdf is saved as a postmeta, you can check if meta exist and then make the conditional:

    $pdf = get_post_meta($post->ID, '_pdf', TRUE);
    if(!empty($pdf)): ?>
        <a href="<?php echo $pdf; ?>">Download</a>
    <?php else: ?>
        <span class="nopdf">No PDF</span>
    <?php ?>