其实我正在使用wp作业管理器插件。该插件只有一个自定义分类 这是在插件中注册分类法的代码
register_taxonomy( "job_listing_type",
array( "job_listing" ),
array(
'hierarchical' => true,
'label' => $plural,
'labels' => array(
'name' => $plural,
'singular_name' => $singular,
'search_items' => sprintf( __( 'Search %s', 'wp-job-manager' ), $plural ),
'all_items' => sprintf( __( 'All %s', 'wp-job-manager' ), $plural ),
'parent_item' => sprintf( __( 'Parent %s', 'wp-job-manager' ), $singular ),
'parent_item_colon' => sprintf( __( 'Parent %s:', 'wp-job-manager' ), $singular ),
'edit_item' => sprintf( __( 'Edit %s', 'wp-job-manager' ), $singular ),
'update_item' => sprintf( __( 'Update %s', 'wp-job-manager' ), $singular ),
'add_new_item' => sprintf( __( 'Add New %s', 'wp-job-manager' ), $singular ),
'new_item_name' => sprintf( __( 'New %s Name', 'wp-job-manager' ), $singular )
),
'show_ui' => true,
'query_var' => true,
'capabilities' => array(
'manage_terms' => $admin_capability,
'edit_terms' => $admin_capability,
'delete_terms' => $admin_capability,
'assign_terms' => $admin_capability,
),
'rewrite' => $rewrite,
)
);
然后我使用
添加了三个分类法function skw_build_taxonomies() {
//Sports
$brand_args = array(
'label' => 'Sports',
'hierarchical' => true,
'query_var' => true,
'rewrite' => array('slug' => 'sport-cat')
);
register_taxonomy( 'sport-cat', 'job_listing', $brand_args );
//Arts
$brand_args1 = array(
'label' => 'Arts & Music',
'hierarchical' => true,
'query_var' => true,
'rewrite' => array('slug' => 'art-cat')
);
register_taxonomy( 'art-cat', 'job_listing', $brand_args1 );
//Academic & Tech
$brand_args2 = array(
'label' => 'Academic & Tech',
'hierarchical' => true,
'query_var' => true,
'rewrite' => array('slug' => 'academic-cat')
);
register_taxonomy( 'academic-cat', 'job_listing', $brand_args2 );
}
add_action( 'init', 'skw_build_taxonomies', 0 );
我还通过更改分类名称和slug来使用插件的代码,而不是这段代码。 现在插入post后,此代码成功设置了分类。 但是当我想通过在前端使用自定义表单来更新帖子时,它会将与分类法job_listing_type相关的术语添加到所有四个分类中并设置它。并正确设置其他三个分类术语。 这是我设置条款的代码。
<input type="checkbox" name="_job_academic[]" value="<?php echo $term->id; ?>" <?php if(in_array($term->slug,$slug)) { echo 'checked="checked"'; } ?> /> <?php echo $term->name ?></span>//sample html in the form
这里是php代码来设置术语
wp_set_post_terms( $postid, $_POST['_job_type'], 'job_listing_type' );
wp_set_post_terms( $postid, $_POST['_job_arts'], 'art-cat' );
wp_set_post_terms( $postid, $_POST['_job_academic'], 'academic-cat' );
wp_set_post_terms( $postid, $_POST['_job_sports'], 'sport-cat' );
我也测试了这个但结果相同
wp_set_object_terms( $postid, $_POST['_job_type'], 'job_listing_type', false );
wp_set_object_terms( $postid, $_POST['_job_arts'], 'art-cat', false );
wp_set_object_terms( $postid, $_POST['_job_academic'], 'academic-cat', false );
wp_set_object_terms( $postid, $_POST['_job_sports'], 'sport-cat', false );
例如,我有四个分类法,名为x1,x2,x3,x4
x1 has terms a, b, c, d
x2 has terms e, f, g, h
x3 has terms i, j, k, l
x4 has terms m, n, o, p
在前端,这些术语显示为复选框。所以假设我已经检查过a,e,i,m个术语。因此,应该为该帖子设置所有这些。 Bet wp_set_post_terms将X1的术语添加到所有其他分类法。这是最终结果
x1 has terms a, b, c, d, d
x2 has terms e, f, g, h, d
x3 has terms i, j, k, l, d
x4 has terms m, n, o, p, d