
How to List of Categories for Custom Post Types In WordPress
You 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!
As per the 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 )
Please Add the below code in functions.php
file.
Now add this below code in template where you want to list.
<?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> ?>