Reputation: 3903
I'm building a Wordpress site using the Advanced Custom Fields
. So now I only want to display these fields if they "exist" e.g. if they have content. I made this conditional statement:
$casemodule = get_field('case_page_instore');
if( $casemodule ) : ?>
<section class="block">
<h2><?php echo $casemodule['case_header'] ?></h2>
<p><?php echo $casemodule['case_subtext'] ?></p>
</section>
<?php endif; ?>
So fa so good, If the fields case_header
and case_subtext
are empty, they will not be shown (obviously), but or some reason the <section>
-tag is always visible, which is kind of strange since I followed the instructions on the ACF page.
Can someone maybe tell me what is going on?
UPDATE
I tried to do the var_dump($casemodule)
and the result was:
array(2) { ["case_header"]=> string(0) "" ["case_subtext"]=> string(0) }
but that didn't solve anything
Upvotes: 1
Views: 3072
Reputation: 3903
I solved it myself, the solution was:
if($casemodule['case_header'] && $casemodule['case_subtext']) : ?>
I simply removed the isset
an voila! :-)
Thanks to Reza for the previous answer
Upvotes: 0
Reputation: 5429
Just because $casemodule['case_header']
and $casemodule['case_subtext']
do not exist, it doesn't mean that $casemodul
won't exist. It could be an array that contains other keys or it could be just an empty array, which will exist!
Try to check if $casemodule['case_header']
and $casemodule['case_subtext']
really exist:
$casemodule = get_field('case_page_instore');
if(isset($casemodule['case_header']) && isset($casemodule['case_subtext'])) : ?>
<section class="block">
<h2><?php echo $casemodule['case_header'] ?></h2>
<p><?php echo $casemodule['case_subtext'] ?></p>
</section>
<?php endif; ?>
If you are unsure about a variable, always use var_dump($variable)
, before you pass it to an if statement.
Upvotes: 1