6

I'm using custom post types in WordPress 3.0 (now 3.0.1) to setup a custom directory system, but can't seem to find how to list out the categories under the custom taxonomy similar to how you'd use the wp_list_categories for normal posts. Anyone know how you'd do this on a page? Thanks!

I've seen suggestions for options like the following, but have had no luck when popping them in there:

<?php
    //list terms in a given taxonomy using wp_list_categories  (also useful as a widget)
    $orderby = 'name';
    $show_count = 0; // 1 for yes, 0 for no
    $pad_counts = 0; // 1 for yes, 0 for no
    $hierarchical = 1; // 1 for yes, 0 for no
    $taxonomy = 'genre';
    $title = '';

    $args = array(
        'orderby' => $orderby,
        'show_count' => $show_count,
        'pad_counts' => $pad_counts,
        'hierarchical' => $hierarchical,
        'taxonomy' => $taxonomy,
        'title_li' => $title
    );
?>
<ul>
<?php wp_list_categories($args); ?>
</ul>

migrated from stackoverflow.com Aug 28 '11 at 14:26

This question came from our site for professional and enthusiast programmers.

  • @Nick please edit the post with your answer – Prifulnath Feb 23 '17 at 12:12
3

Are you sure you called register_taxonomy before you called wp_list_categories? register_taxonomy should be called in the init action hook, your template code (I assume you use it there) after that.

2

you have to post some content associated to your taxonomy[custom taxonomy] first! I used you code in my project and works like a charm!

0

If you need something more like ordering, filtering and pagination, see my post : http://geryit.com/blog/2011/03/wordpress-custom-post-types/

0

as per below code my custom taxonomy name is "eventcategory" , change this as per yours and add below code and enjoy , it will create select list of custom taxonomy terms (Note : Add custom taxonomy terms in admin )

Add below code in functions.php file

<?php 
if ( ! function_exists( 'get_terms_dropdown_ministry' ) ) :
function get_terms_dropdown_ministry($taxonomies, $args){
    $myterms = get_terms($taxonomies, $args);
    $output ="<select name='eventcategory'>";
    $output .="<option value='ministries'>".esc_attr(__('Filter by ministry'))."</option>";

       $queried_object = get_queried_object();
       $term_id = $queried_object->term_id;
       $mterm = get_term( $term_id , $taxonomy );
       $tslug = $mterm->slug;
 //exit;

    foreach($myterms as $term){
        $root_url = get_bloginfo('url');
        $term_taxonomy=$term->taxonomy;
        $term_slug=$term->slug;
        if($tslug == $term_slug){
            $selected = "selected='selected'";
        }else{
            $selected = "";
        }
        $term_name =$term->name;
        $link = $term_slug;
        $output .="<option value='".$link."' $selected>".$term_name."</option>";
    }
    $output .="</select>";
return $output;
}
endif;
?>

Add below code in template where you want to list.

<?php
  $event_cat_term = 'eventcategory'; 
  ?>  
  <form action="<?php bloginfo('url'); ?>" method="get">
        <?php
        $taxonomies = array($event_cat_term);
        $args = array('orderby'=>'name','hide_empty'=>true);
        $select = get_terms_dropdown_ministry($taxonomies, $args);

        $select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
        echo $select;
        ?>
            <noscript><input type="submit" value="View" /></noscript>
  </form>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy