How to display custom taxonomies with links in filter menu?

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):

Read More
<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!

Related posts

Leave a Reply

1 comment

  1. <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','taxonomy'=>'galleries');
        $galleries=get_categories($args);
        foreach($galleries as $gallery){ ?>
        <li><a href="<?php echo get_category_link( $gallery->term_id ) ; ?>" class="<?php echo $gallery->gallery_nicename; ?>"><?php echo $gallery->name; ?></a></li>
        <?php } ?>
    </ul>
    <div class="clearfix"></div>
    

    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.

    $custom_slug = get_option('slug') != '' ? get_option('slug') : 'portfolio';
    
        $args = array(
            'labels'            => array(
                'name'                  => __('Portfolio'),
                'singular_name'         => __('Portfolio Project'),
                'add_new'               => __('Add Project'),
                'add_new_item'          => __('Add Project'),
                'new_item'              => __('Add Project'),
                'view_item'             => __('View Project'),
                'search_items'          => __('Search Portfolio'), 
                'edit_item'             => __('Edit Project'),
                'all_items'             => __('Complete Portfolio'),
                'not_found'             => __('No Projects found'),
                'not_found_in_trash'    => __('No Projects found in Trash')
            ),
            'taxonomies'        => array('portfolio-categories', 'portfolio-clients', 'portfolio-tags'),
            'public'            => true,
            'show_ui'           => true,
            '_builtin'          => false,
            '_edit_link'        => 'post.php?post=%d',
            'capability_type'   => 'post',
            'rewrite'           => array('slug' => __($custom_slug)),
            'hierarchical'      => false,
            'menu_position'     => 20,
            'menu_icon'         => WP_PLUGIN_URL . '/portfolio/images/icon.jpg',
            'supports'          => array('title', 'editor')
        );
    
        /** create portfolio categories (taxonomy) */
        register_taxonomy('portfolio-categories', 'project', array(
                'hierarchical'      => true,
                'show_ui'           => true,
                'rewrite'           => array('slug' => __($custom_slug . '/category')),
                'labels'            => array(
                        'name'                          => __('Portfolio Categories'),
                        'singular_name'                 => __('Portfolio Category'),
                        'search_items'                  => __('Search Portfolio Categories'),
                        'popular_items'                 => __('Popular Portfolio Categories'),
                        'all_items'                     => __('All Portfolio Categories' ),
                        'parent_item'                   => __('Parent Portfolio Category'),
                        'parent_item_colon'             => __('Parent Portfolio Category'),
                        'edit_item'                     => __('Edit Portfolio Category'), 
                        'update_item'                   => __('Update Portfolio Category'),
                        'add_new_item'                  => __('Add New Portfolio Category'),
                        'new_item_name'                 => __('New Portfolio Category'),
                        'separate_items_with_commas'    => __('Separate Portfolio Categories with commas'),
                        'add_or_remove_items'           => __('Add or remove Portfolio Categories'),
                        'choose_from_most_used'         => __('Choose from the most used Portfolio Categories')
            )
        ));
    
    
    
    
        /** create new custom post type */
        register_post_type('portfolio', $args);