简体   繁体   中英

Wordpress show tags belonging to custom post type

I created a custom post type called "portfolio" and added the tags column in editor using:

register_taxonomy_for_object_type( 'post_tag', 'portfolio'  );

After I add the tags widget to the sidebar it shows all the tags from all my posts. How can I show tags that belong only to the "portfolio" post type?

Also is there a way to change the default name "Tags" for the portfolio post type to "Portfolio Tags"?

The easiest way of achieving that would probably be to create a custom taxonomy. That is also the only way to change the name of your tags to Portfolio Tags.

To do that, you can use register_taxonomy():

// Register Custom Taxonomy
function portfolio_tags_taxononmy() {

    $labels = array(
        'name'                       => 'Portfolio Tag',
        'singular_name'              => 'Portfolio Tag',
        'menu_name'                  => 'Portfolio Tags',
        'all_items'                  => 'All Portfolio Tags',
        'parent_item'                => 'Parent Portfolio Tag',
        'parent_item_colon'          => 'Parent Portfolio Tag:',
        'new_item_name'              => 'New Portfolio Tag',
        'add_new_item'               => 'Add New Portfolio Tag',
        'edit_item'                  => 'Edit Portfolio Tag',
        'update_item'                => 'Update Portfolio Tag',
        'separate_items_with_commas' => 'Separate Portfolio Tags with commas',
        'search_items'               => 'Search Portfolio Tags',
        'add_or_remove_items'        => 'Add or remove Portfolio Tags',
        'choose_from_most_used'      => 'Choose from the most used Portfolio Tags',
        'not_found'                  => 'Not Found',
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => false,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
    );
    register_taxonomy( 'portfolio-tags', array( 'portfolio' ), $args );

}

// Hook into the 'init' action
add_action( 'init', 'portfolio_tags_taxononmy', 0 );

Now, you can use 'portfolio-tags' instead of 'post_tag':

register_taxonomy_for_object_type( 'portfolio-tags', 'portfolio' );

To generate your tag cloud, you can use wp_tag_cloud():

wp_tag_cloud( array( 'taxonomy' => 'portfolio-tags' ) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM