Home › Forums › General Issues › 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>
</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
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 } ?>
Also you could refer to Using conditional statements on the following page: http://www.advancedcustomfields.com/resources/getting-started/code-examples/
Not only has this solve the issue, it has helped me understand what i did incorrectly.
Thanks you!
The topic ‘If statement not working’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.