是否可以使用自定义字段分配职位类别?
例如:
我有Genres
自定义字段到我的自定义帖子类型Tvseries
.
然后我进入了:动作、戏剧、喜剧
保存或发布后,它将与我在输入/自定义字段中输入的3个类别一起分配。
是否可以使用自定义字段分配职位类别?
例如:
我有Genres
自定义字段到我的自定义帖子类型Tvseries
.
然后我进入了:动作、戏剧、喜剧
保存或发布后,它将与我在输入/自定义字段中输入的3个类别一起分配。
你应该加入save_post
并使用wp_set_object_terms()
:
// Add an action to run on post save
add_action( \'save_post\', \'set_genre_on_save\' );
function set_genre_on_save( $post_id ){
// Check the post type
if (is_single(\'tvseries\')) {
// Get the custom field data
$custom_field_data = get_post_custom( $post_id );
// Check if there is any genres entered in the metabox
if (isset($custom_box_data[\'genre\'])) {
// Save the genre data (separated by comma) into an array
$genre_array = explode( \',\', $custom_box_data[\'genre\'] );
//Set the array values to lower case
foreach ($genre_array as $genre){
$genre = strtolower($genre);
}
// Set the categories for these genres
wp_set_object_terms( $post_id, $genre_array, \'category\' );
}
}
}
您应该输入slug
或者ID
您在该领域的类别。例如War Movies
不起作用,但是war-movies
将起作用。此外,值之间不应该有空格(或者您必须更改\',\'
到\' ,\'
).请注意,这只是一个示例,因为您没有发布任何代码。您可能需要更改某些值,例如genres
以匹配自定义字段/帖子类型。
我正在使用wp\\u dropdown\\u categories向媒体库添加类别过滤器。它工作正常,除了显示分配给帖子的所有类别,但我希望它只显示分配给附件的类别。如果只为附件更新计数也很好。下面是将类别添加到附件并允许您对其进行筛选的代码。/* Add categories to attachments/media library */ function wptp_add_categories_to_attachments() { register_taxonomy_for_objec