【问题标题】:Set flag value to "0" if category ID is in_array (Wordpress)如果类别 ID 为 in_array (Wordpress),则将标志值设置为“0”
【发布时间】:2016-06-13 22:52:42
【问题描述】:

如果类别 549 在 $adsense_block_post 中,我正在尝试将 $ad_flg 值设置为“0”

<?php Global $ad_flg; $ad_flg=1; ?>
<?php $adsense_blck_category = array(549); ?> 
<?php if (array_key_exists  ($_category_id->ID,$adsense_blck_category,true)) $ad_flg=0; ?>`

有什么建议吗?

【问题讨论】:

    标签: php wordpress categories adsense


    【解决方案1】:

    您正在检查数组 keys,而不是值。请改用in_array()

    <?php 
    
    $adsense_blck_category = array(549);
    $ad_flg = ! ( in_array($_category_id->ID, $adsense_blck_category) );
    
    ?>
    

    正如您将看到的,您的代码也可以通过简单地将 in_array() 的相反返回值分配给您的标志(使用 ! 运算符)来大大简化。

    Demo

    【讨论】:

    • 是的,但是如何在 if 语句中检查该值?
    • 不将其分配给变量,只需将其包装在 if() 块中:if( ! ( in_array($_category_id-&gt;ID, $adsense_blck_category) ) )
    猜你喜欢
    • 2020-11-07
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 2011-11-25
    • 1970-01-01
    相关资源
    最近更新 更多