Since two days I am trying to solve a problem with custom taxonomies (categories).
I have a Custom Post Type called ‘Portfolio’ and it works as expected but a problem is with my hierarchical taxonomy that I have registered. I gave it a name: ‘galleries’. Instead of displaying custom taxonomies it simply shows default categories from my blog. I found some similar problems on here but I realy don’t know how to adopt other’s solutions into my case. I am quite new to wordpress unfortunately. Can someone help me?
This is the code from my loop.php which is the template of my custom post type (this is my home page):
<div id="filtering-nav">
<a href="#" class="filter-btn"><span>Filter</span></a>
<ul>
<li><a href="#all" class="all">All</a></li>
<?php $args=array('orderby' => 'name');
$galleries=get_categories($args);
foreach($galleries as $gallery){ ?>
<li><a href="#<?php echo $gallery->gallery_nicename; ?>" class="<?php echo $gallery->gallery_nicename; ?>"><?php echo $gallery->name; ?></a></li>
<?php } ?>
</ul>
<div class="clearfix"></div>
Here is the code from my function.php:
add_action('init', 'portfolio_register');
function portfolio_register() {
$labels = array(
'name' => _x('My Portfolio', 'post type general name'),
'singular_name' => _x('Portfolio Item', 'post type singular name'),
'add_new' => _x('Add New', 'portfolio item'),
'add_new_item' => __('Add New Portfolio Item'),
'edit_item' => __('Edit Portfolio Item'),
'new_item' => __('New Portfolio Item'),
'view_item' => __('View Portfolio Item'),
'search_items' => __('Search Portfolio'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => get_stylesheet_directory_uri() . '/images/portfolio-icon.png',
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','author','thumbnail','comments')
);
register_post_type( 'portfolio' , $args );
flush_rewrite_rules();
}
// Custom taxonomy for Portfolio Categories (Galleries)
register_taxonomy('galleries', array('portfolio'), array('hierarchical' => true, 'label' => 'Galleries', 'singular_label' => 'Gallery', 'rewrite' => true, 'public' => true ));
Thanks in advance!
When you pass argument to get_categories function , you need to pass name of taxonomy as taxonomy parameter to retrieve taxonomy instead of categories…
Here is complete reference..
http://codex.wordpress.org/Function_Reference/get_categories
Compare your code to below, you are missing taxonomies in args in register post type and rewrite slug in custom taxonomy arg. Below code is working perfectly.