Support

Account

Home Forums General Issues If statement not working

Solved

If statement not working

    • Paradox

    • July 10, 2013 at 10:02 am

    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>
    </div>
    <?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') != " ) { ?>
    <h4>Resources:</h4>
    <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

    • rmdev

    • July 10, 2013 at 11:08 am

    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.

    
    <?php 
    $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>
        </div>
    <?php } ?>
    
    
    <?php 
    $linksResources = get_field('links_and_resources'); 
    if($linksResources) { ?>
        <h4>Resources:</h4>
        <p><?php echo $linksResources; ?></p>
    <?php } ?>
    
    • rmdev

    • July 10, 2013 at 11:10 am

    Also you could refer to Using conditional statements on the following page: http://www.advancedcustomfields.com/resources/getting-started/code-examples/

    • Paradox

    • July 10, 2013 at 11:21 am

    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)

You must be logged in to reply to this topic.

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.