在下面的代码中,当检查foreach(get_sub_field(“items”)为$ x时):
如何检查$ x ['item_name'];是空的,这样做......
<?php $food = 23; ?>
<?php while(has_sub_field("menu_items", $food)): ?>
<?php if(get_row_layout() == 'food_items'): ?>
<?php if (get_sub_field('category')){?>
<h2>
<?php the_sub_field('category');?>
</h2>
<?php } ?>
<div>
<?php if(get_sub_field("items")): ?>
<?php foreach(get_sub_field("items") as $x): ?>
<div> <?php echo $x['item_name']; ?> <?php echo $x['item_price']; ?>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>
答案 0 :(得分:0)
尝试empty()
<?php echo (!empty($x['item_name']) ? $x['item_name'] : 'do this'); ?>
或
if(!empty($x['item_name'])) {
echo $x['item_name'];
}
else {
// do your stuff
}
答案 1 :(得分:0)
你可以试试这个:
<?php if (empty($x['item_name'])) : ?>
<p>It's empty</p>
<?php endif; ?>