我使用以下代码将所有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);
}
}
我想添加一个名为' business-information'的其他类别的例外情况。但我无法让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);
}
}
答案 0 :(得分:1)
您错误地使用了if (!in_category('sliders')) OR (!in_category('business-information')) {
这样写(@FirstOne提到的另一件事你需要使用AND
而不是OR
来应用这两个条件而不是其中一个条件。)
if( !in_category('sliders') AND !in_category('business-information') ) {
...
}
这样两个!in_category
检查都将处于相同的if( ... )
scops