Registering tags taxonomy for a custom post type

The actual post type support categories and I want to add also support for tags.

I have this code:

register_taxonomy(
    'category_' . $slug,
    array( $slug ), 
    array(
        'hierarchical' => true,
        'label' => "$slug Categories",
        'singular_label' => "$slug Category",
        'rewrite' => true
    )
);

So, I was trying to duplicate this for supporting tags but is not working.

This is the entire code for portfolio post type :

$portfolio_slugs = get_option("zeitgeist_portfolio_slug");
if(is_array($portfolio_slugs))
    foreach ( $portfolio_slugs as $slug ){
        add_action('init', 'create_portfolio');
        register_taxonomy("category_".$slug, array($slug), 
        array("hierarchical" => true, "label" => "$slug Categories", "singular_label" => "$slug Category", "rewrite" => true));
        register_taxonomy("tags_".$slug, array($slug), array("hierarchical" => false, "label" => "$slug Tags", "singular_label" => "$slug Tags", "rewrite" => true));



function create_portfolio() {
    $portfolio_slugs = get_option("zeitgeist_portfolio_slug");
    $portfolio_counter = 0;
    $portfolio_names = get_option("zeitgeist_portfolio_name");
    foreach ( $portfolio_slugs as $slug ){
        $portfolio_args = array(
            'label' => __("Portfolio '".$portfolio_names[$portfolio_counter]."'"),
            'singular_label' => __($portfolio_names[$portfolio_counter++]),
            'public' => true,
            'show_ui' => true,
            'capability_type' => 'post',
            'hierarchical' => false,
            'rewrite' => array('slug' => $slug, 'with_front' => true),
            'supports' => array('title', 'editor', 'thumbnail', 'author', 'comments', 'excerpt')
        );
        register_post_type($slug,$portfolio_args);
    }
}

function portfolioSingleRedirect(){
    global $wp_query;
    $queryptype = $wp_query->query_vars["post_type"];
    $portfolio_slugs = get_option("zeitgeist_portfolio_slug");
    if(is_array($portfolio_slugs))
        foreach ( $portfolio_slugs as $slug ){
            if ($queryptype == $slug){
                if (have_posts()){
                    global $pcat;
                    $pcat = "category_".$slug;
                    require(TEMPLATEPATH . '/single_portfolio.php');
                    die();
                }else{
                    $wp_query->is_404 = true;
                }
            }
        }
}
add_action("template_redirect", 'portfolioSingleRedirect');

2 Answers
2

The categories your code creates is a custom taxonomy, not the default post category taxonomy.

Anyway, if you want to add support for the default post tags taxonomy, the name is post_tag and can be added via the taxonomies argument in your $portfolio_args:

$portfolio_args = array(
    'taxonomies' => array( 'post_tag' ),
    // all of your other portfolio args
);

See register_post_type in Codex for more info.

Leave a Comment