2

I'm trying to hide all the html if my custom fields are empty in Wordpress

I'm using this:

<?php if (the_field( 'attribute_1' ) & the_field( 'value_1' ) != "") { ?>
<li style="line-height:2.25em;border-top:1px solid #dcdcdc;"><p style="font-size:15px;margin-bottom:0;"><?php the_field( 'attribute_1' ); ?> <span style="float:right;font-weight:600;font-family:'Varela Round';"><?php the_field( 'value_1' ); ?></span></p></li>
<?php } ?>

But only 1 custom field shows up, any ideas why? I've looked into it quite a bit but can't figure it out

4
  • 1
    What are the values of the_field( 'attribute_1' ) and the_field( 'value_1' )? You should probably be using &&. Commented Feb 11, 2016 at 2:51
  • The values are just regular text, currently asdasd for attribute_1 and asd for attribute_2 when I use && only attribute_1 displays Commented Feb 11, 2016 at 2:53
  • You mean to use && - that's correct. Yours is not correct. Commented Feb 11, 2016 at 3:01
  • Ah yes, I do use &&. I'm sorry I'm tired I pasted wrong. So with &&, I only see 1 custom field. Commented Feb 11, 2016 at 3:05

2 Answers 2

1

assuming that your fields are correct (attribute_1 and value_1), then the issue with the code is the use of the incorrect functions.

the_field outputs the contents of the field.

In your if condition, you need to use get_field which returns the contents of the field:

<?php if (get_field( 'attribute_1' ) && get_field( 'value_1' ) != "") { ?>
    <li style="line-height:2.25em;border-top:1px solid #dcdcdc;">
        <p style="font-size:15px;margin-bottom:0;"><?php the_field( 'attribute_1' ); ?> <span style="float:right;font-weight:600;font-family:'Varela Round';"><?php the_field( 'value_1' ); ?></span>
        </p>
    </li>
<?php } ?>
Sign up to request clarification or add additional context in comments.

1 Comment

you have no idea how happy you've made me, thanks for the help and the explanation. now i know for future reference! thanks thanks!!
0

Since you have not provided details about the methods listed above, you could try something like this.

<?php
if(isset("myfield") && isset("myfield2")){
   echo "<input type=\"text\" id=\"customField1\"></input>";
} //repeat as necessary
?>

If you are banking on 'myfield' being variables from a different page, then you should use:

<?php //use $_GET if necessary
if(isset($_POST['myfield']) && isset($_POST['myfield2'])){
   echo "<input type=\"text\" id=\"customField1\"></input>";
} //repeat as necessary
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.