How to Add Custom Category and Tag For Post Type
I’m currently site manager for phone components, in-process sales page I met this pretty little buggy though not great. Now before I make the generic product tag to post, but today discovered the tag still exists but can not currently access products in this tag. Only new post accessible, squirming almost 20 minutes to fix the errors, post them here for you reference this painful experience.
to activate functions for custom post type tag when you add the following declaration to the function “register_post_type” Look, for example, for ease:
add_action('init', 'create_product_post_type');
function create_product_post_type()
{
register_post_type('product',
array(
'labels' => array(
'name' => __('Sản Phẩm'),
'singular_name' => __('Sản Phẩm'),
'add_new' => __('Thêm SP'),
'add_new_item' => __('Thêm SP'),
'edit' => __('Chỉnh Sửa'),
'edit_item' => __('Chỉnh Sửa'),
'new_item' => __('Thêm SP'),
'view' => __('Xem Sản Phẩm'),
'view_item' => __('Xem Sản Phẩm'),
'search_items' => __('Tìm Sản Phẩm'),
'not_found' => __('Không tìm thấy sản phẩm nào'),
'not_found_in_trash' => __('Không có sản phẩm nào trong thùng rác')
),
'public' => true,
'show_ui' => true,
'publicy_queryable' => true,
'exclude_from_search' => false,
'menu_position' => 20,
'rewrite' => array('slug' => false, 'with_front' => false),
'menu_icon' => get_stylesheet_directory_uri(). '/images/cart.png',
'hierarchical' => false,
'query_var' => true,
'supports' => array(
'title', 'editor', 'comments', 'author', 'excerpt', 'thumbnail', 'tag', 'custom-fields'
),
//'taxonomies' => array('post_tag', 'category'),
'can_export' => true,
//'register_meta_box_cb' => 'call_to_function_do_something',
'description' => __('Mô tả sản phẩm ở đây.'),
'taxonomies' => array('post_tag')
)
);
}
in the above code is the most important part is the “‘taxonomies’ => array (‘ post_tag ‘)” if you want to embed more numerous category is changed to “‘ taxonomies’ => array (‘category’, ‘post_tag ‘) ”
The following is the code for the query post it will take up the remaining custom post
function wpa_cpt_tags( $query ) {
if ( $query->is_tag() && $query->is_main_query() ) {
$query->set( 'post_type', array( 'post', 'product' ) );
}
}
add the above code is that when you visit the display tag will always carry both custom post type tag it, too kool.
If you select a category, then add more conditions “is_category” in the above code into offline