我为一个帖子类型创建了分类法,它们很有效。问题是,当我在前端添加一篇新文章(在文章类型中)时,默认情况下会显示我在前一篇文章中输入的分类术语。实际上,新职位的分类术语不会重置。是否有此功能?我如何解决?
添加帖子类型
if ( ! function_exists(\'bookmaker_post_type\') ) {
// Register Custom Post Type
function bookmaker_post_type() {
$labels = array(
\'name\' => _x( \'Bookmakers\', \'Post Type General Name\', \'text_domain\' ),
\'singular_name\' => _x( \'Bookmaker\', \'Post Type Singular Name\', \'text_domain\' ),
\'menu_name\' => __( \'Bookmakers\', \'text_domain\' ),
\'name_admin_bar\' => __( \'Bookmakers Reviews\', \'text_domain\' ),
\'archives\' => __( \'Item Archives\', \'text_domain\' ),
\'attributes\' => __( \'Item Attributes\', \'text_domain\' ),
\'parent_item_colon\' => __( \'Parent Item:\', \'text_domain\' ),
\'all_items\' => __( \'All Bookmakers\', \'text_domain\' ),
\'add_new_item\' => __( \'Add New Bookmaker\', \'text_domain\' ),
\'add_new\' => __( \'Add New Bookmaker\', \'text_domain\' ),
\'new_item\' => __( \'New Bookmaker\', \'text_domain\' ),
\'edit_item\' => __( \'Edit Bookmaker\', \'text_domain\' ),
\'update_item\' => __( \'Update Item\', \'text_domain\' ),
\'view_item\' => __( \'View Item\', \'text_domain\' ),
\'view_items\' => __( \'View Items\', \'text_domain\' ),
\'search_items\' => __( \'Search Item\', \'text_domain\' ),
\'not_found\' => __( \'Not found\', \'text_domain\' ),
\'not_found_in_trash\' => __( \'Not found in Trash\', \'text_domain\' ),
\'featured_image\' => __( \'Featured Image\', \'text_domain\' ),
\'set_featured_image\' => __( \'Set featured image\', \'text_domain\' ),
\'remove_featured_image\' => __( \'Remove featured image\', \'text_domain\' ),
\'use_featured_image\' => __( \'Use as featured image\', \'text_domain\' ),
\'insert_into_item\' => __( \'Insert into item\', \'text_domain\' ),
\'uploaded_to_this_item\' => __( \'Uploaded to this item\', \'text_domain\' ),
\'items_list\' => __( \'Items list\', \'text_domain\' ),
\'items_list_navigation\' => __( \'Items list navigation\', \'text_domain\' ),
\'filter_items_list\' => __( \'Filter items list\', \'text_domain\' ),
);
$args = array(
\'label\' => __( \'Bookmaker\', \'text_domain\' ),
\'description\' => __( \'Bookmaker review\', \'text_domain\' ),
\'labels\' => $labels,
\'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'revisions\', \'custom-fields\' ),
\'hierarchical\' => false,
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'menu_position\' => 5,
\'show_in_admin_bar\' => true,
\'show_in_nav_menus\' => true,
\'can_export\' => true,
\'has_archive\' => true,
\'exclude_from_search\' => false,
\'publicly_queryable\' => true,
\'capability_type\' => \'page\',
);
register_post_type( \'bookmaker\', $args );
}
add_action( \'init\', \'bookmaker_post_type\', 0 );
}
添加分类法
function bookmaker_taxonomies() {
// Add new taxonomy Languages, make it hierarchical (like categories)
$labels = array(
\'name\' => _x( \'Languages\', \'taxonomy general name\', \'textdomain\' ),
\'singular_name\' => _x( \'Language\', \'taxonomy singular name\', \'textdomain\' ),
\'search_items\' => __( \'Search Languages\', \'textdomain\' ),
\'all_items\' => __( \'All Languages\', \'textdomain\' ),
\'parent_item\' => __( \'Parent Language\', \'textdomain\' ),
\'parent_item_colon\' => __( \'Parent Language:\', \'textdomain\' ),
\'edit_item\' => __( \'Edit Language\', \'textdomain\' ),
\'update_item\' => __( \'Update Language\', \'textdomain\' ),
\'add_new_item\' => __( \'Add New Language\', \'textdomain\' ),
\'new_item_name\' => __( \'New Language Name\', \'textdomain\' ),
\'menu_name\' => __( \'Language\', \'textdomain\' ),
);
$args = array(
\'hierarchical\' => true,
\'labels\' => $labels,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'language\' ),
);
register_taxonomy( \'language\', array( \'bookmaker\' ), $args );
unset( $args );
unset( $labels );
add_action( \'init\', \'bookmaker_taxonomies\', 0 );
用于在前端显示分类术语的函数
function get_term_list($taxonomy){
$terms = get_terms($taxonomy);
echo \'\';
foreach ( $terms as $term ) {
// The $term is an object, so we don\'t need to specify the $taxonomy.
$term_link = get_term_link( $term );
// If there was an error, continue to the next term.
if ( is_wp_error( $term_link ) ) {
continue;
}
// We successfully got a link. Print it out.
echo \'<a href="\' . esc_url( $term_link ) . \'">\' . $term->name . ", ",\'</a>\';
}
echo \'\';
}
检索分类术语并在前端显示
<div><?php get_term_list(\'language\'); ?></div>