PHP IF OR语句验证错误


PHP IF OR statement validation error

Im使用以下代码将Wordpress的所有帖子(不包括"滑块"类别)添加到名为"Frontpage"ID=28 的类别中

function add_category_automatically1($post_ID) {
global $wpdb;
$postsWeWants = $wpdb->get_results("SELECT ID, post_author FROM $wpdb->posts where ID = $post_ID");
foreach ($postsWeWants as $postsWeWant) {
    if (!in_category('sliders'))  {
        $cat = array(28, );
        wp_set_object_terms($post_ID, $cat, 'category', true);
    }
}

我想添加一个名为"业务信息"的附加类别的例外,但我无法使OR运算符正确验证。

我正在考虑使用类似下面的东西

function add_category_automatically1($post_ID) {
global $wpdb;
$postsWeWants = $wpdb->get_results("SELECT ID, post_author FROM $wpdb->posts where ID = $post_ID");
foreach ($postsWeWants as $postsWeWant) {
    if (!in_category('sliders')) OR (!in_category('business-information')) {
        $cat = array(28, );
        wp_set_object_terms($post_ID, $cat, 'category', true);
    }
}

这:

if (!in_category('sliders')) OR (!in_category('business-information'))
//                         ^ -- ^ -- and here

是错误的。因为您过早关闭)并打开(。正确的代码是:

if (!in_category('sliders') OR !in_category('business-information'))

顺便说一下,这样的逻辑是无效的。如果项目在'business-information'中,则!in_category('sliders')为true。我想你需要检查是否不存在于两只猫中:

if (!in_category('sliders') AND !in_category('business-information'))

您用错了if (!in_category('sliders')) OR (!in_category('business-information')) {

这样写(@FirstOne提到的另一件事是,你需要使用AND而不是OR来应用这两个条件,而不是其中之一)

if( !in_category('sliders')  AND !in_category('business-information') ) {
...
}

以便两个!in_category检查将在相同的if( ... )作用域