Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
自动将wordpress类别分配给post类_Wordpress_Wordpress Theming_Categories - Fatal编程技术网

自动将wordpress类别分配给post类

自动将wordpress类别分配给post类,wordpress,wordpress-theming,categories,Wordpress,Wordpress Theming,Categories,是否可以创建一个类似于此的函数 function add_category_automatically($post_ID) { global $wpdb; $postsWeWants = $wpdb->get_results("SELECT ID, post_author FROM $wpdb->posts where ID = $post_ID"); foreach ($postsWeWants as $postsWeWant) {

是否可以创建一个类似于此的函数

function add_category_automatically($post_ID) {  
    global $wpdb;  
    $postsWeWants = $wpdb->get_results("SELECT ID, post_author FROM $wpdb->posts where ID = $post_ID");  
    foreach ($postsWeWants as $postsWeWant) {  
        if(($postsWeWant->post_author != 30) && ($postsWeWant->post_author != 29) && !in_category('bundle')){  
            $cat = array(9547,9742);  
            wp_set_object_terms($post_ID, $cat, 'category', true);  
        }  
    }  
}  
add_action('publish_post', 'add_category_automatically');

但是基于“而不是作者和类别”?

类似的东西应该可以做到:

function add_category_by_class($post_ID) {  
  if( in_array( 'classToCheckFor' get_post_class('', $post_ID)) ){
    $cat = array(9547,9742);
    wp_set_object_terms($post_ID, $cat, 'category', true); 
  }       
}  
add_action('publish_post', 'add_category_by_class');

谢谢!!但这让我想到了另一个问题。你知道为什么一个类出现在firebug中而没有出现在firebug中吗?可能是因为它是在一些模板文件中硬编码的,或者是由一些钩子添加的,WP有很多。。。我们讨论的是哪个类?这很有效,谢谢:函数add_category_by_class($post_ID){if(isset($post['classtocheckforr']){$cat=array(95479742);wp_set_object_terms($post_ID,$cat,'category',true)}add_action($publish_post,'add category_by_class');