Home Forums General Issues If statement not working


If statement not working

  • Hi,

    I following the instructions I found for if statements with the_field (don’t remember now where I saw them), and added them in a php enabled widget.

    Here is the code

    <?php if ( the_field('downloadable_material') != ") { ?>
    <div class="material">
    <h4>Supplemental Material:</h4>
    <a href="<?php the_field('downloadable_material'); ?>"><i class="icon-download-alt"></i> - Download Supplemental Material</a>
    <?php } else { }; ?>

    If downloadable_material (a file attachement field) has a file, it shows up and links correctly.
    if it is empty the whole thing still shows up on the page, (but with an empty link).

    The similar thing is happening with another statement in the same widget:

    <?php if ( the_field('links_and_resources') != " ) { ?>
    <p><?php the_field('links_and_resources'); ?></p>
    <?php } else { }; ?>

    Now this second one works in a strange way. it shows nothing when empty and shows the content of the field when it has something but it does not show the h4 “Resources:” at all. and if I add the <div class="material"></div> wrapped around the two lines between the if statement it generates the same error as below.

    I assumed the if statement was not written correctly. but any change I make to it results in the following error:

    Parse error: syntax error, unexpected T_STRING in execphp.php(44) : eval()’d code on line 5

    I have been breaking my brain on this for hours. I hope someone here can help.

    Thanks in advance

  • First off, the_field is used to echo a field’s contents, get_field is used to retrieve the fields data into into a variable, or in your case, can be used in an if statement. You also have a semi-colon after an if statement which is invalid and an empty else may had well just be removed. I’ve tied up your code a bit, give it a try.

    $dlMaterials = get_field('downloadable_material'); 
    if($dlMaterials) { ?>
        <div class="material">
            <h4>Supplemental Material:</h4>
            <a href="<?php echo $dlMaterials; ?>"><i class="icon-download-alt"></i> - Download Supplemental Material</a>
    <?php } ?>
    $linksResources = get_field('links_and_resources'); 
    if($linksResources) { ?>
        <p><?php echo $linksResources; ?></p>
    <?php } ?>
  • Also you could refer to Using conditional statements on the following page:

  • Not only has this solve the issue, it has helped me understand what i did incorrectly.

    Thanks you!

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘If statement not working’ is closed to new replies.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.