[英]custom post type default category wordpress
I need to assign a default category to my custom post type. 我需要为自定义帖子类型分配默认类别。
i created this, but do not work: 我创建了这个,但是不起作用:
function add_portfolio_category_automatically($post_ID) {
global $wpdb;
if(!has_term(”,’portfolio_category’,$post_ID)){
$cat = array(11);
wp_set_object_terms($post_ID, $cat, ‘portfolio_category’);
}
}
add_action(‘avada_portfolio’, ‘add_portfolio_category_automatically’);
Information: 信息:
custom post type taxonomies: avada_portfolio 自定义帖子类型分类法:avada_portfolio
custom post type taxonomies of category: portfolio_category 类别的自定义帖子类型分类法:Portfolio_category
I wish all the posts had the category "car" as defaul. 我希望所有帖子的默认类别均为“汽车”。
I tried for hours without success. 我试了几个小时都没有成功。
You have to pass the "car" category slug or ID as second parameter of wp_set_object_terms. 您必须传递“汽车”类别的子段或ID作为wp_set_object_terms的第二个参数。
Try 尝试
wp_set_object_terms($post_ID, "car", ‘portfolio_category’);
If "car" is not your term slug, retrieve the term ID first. 如果“ car”不是您的术语,请首先获取术语ID。
$term = get_term_by( 'name', 'car', 'portfolio_category' );
$term_id = (int) $term->term_id;
wp_set_object_terms($post_ID, $term_id, ‘portfolio_category’);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.