为什么我的字符串变量被识别为空



我有一个Wordpress的PHP主题编辑器的输入,并将其保存到某个变量中。

如果变量不是空变量,我想呈现(回显(出该变量。

如果我不使用如果,它会正常工作(这证明变量不是空的(

<div class="role"><?php echo get_post_meta( get_the_ID(), 'hcf_single_role_1', true );?></div>

但当我想回显唯一有值的变量时,它会起作用,否则

<?php if(!empty($post_id['hcf_single_role_1'])) { ?> //if not empty
<div class="name"><?php echo get_post_meta( get_the_ID(), 'hcf_single_role_1', true );?></div>
<?php } else { ?> //if empty
Variable Empty
<?php } ?>

如果我更改为if(empty()),它将以其他方式工作,我的变量被识别为空变量。

*编辑:我也试过if(!empty($_POST['hcf_single_role_1'])),但它是相同的

基于@ChrisHaas的评论,我尝试了这种方法,它能在中工作

<?php if(!empty(get_post_meta( get_the_ID(), 'hcf_single_role_1', true ))) { ?> //if not empty
<div class="name"><?php echo get_post_meta( get_the_ID(), 'hcf_single_role_1', true );?></div>
<?php } else { ?> //if empty
Variable Empty
<?php } ?>

之前的方法在数组和字符串变量之间搞砸了。

所以我用与get_post_meta()相同的方法清理,作为if条件

最新更新