• I have a custom post type and am building the single-.php template for it.

    As far as pulling custom fields and everything, the single page is working fine.

    The one problem is that I want to display a facebook link only if that field has been filled in. For my testing I wrote an if statement with simple html in it to see if the logic would work.

    The custom field can be pulled using <?php the_field('facebook_link'); ?>

    For instance, if I wanted to make that a link, I can make it work by using

    <a href="<?php the_field('facebook_link'); ?>">Facebook</a>

    The if statement that I tried out is as follows:

    <?php
    if ( the_field('facebook_link') !== '' ) {
        ?> <p>HTML GOES HERE</p> <?php;
    }
    ?>

    This is assuming that once I get the logic working correctly, I can replace the HTML GOES HERE with the link as I have structured above.

    The problem is that on a post that has a facebook link filled in, it is outputting the link and then the html, like this:

    https://facebook.com/wordpress HTML GOES HERE

    On a post that doesn’t have a facebook link, it is just outputting the html goes here part.

    Any help with this would be appreciated. I’m fairly new to php and especially when moving between php and html in the page template, I think I am missing a few things.

    Thank you

  • The topic ‘Display custom field on custom page template only if the field has a value’ is closed to new replies.