WordPress: Remove "Parent" dropdown from category form - wordpress

Hi I've downloaded a plugin "Simple Staff List" and it does what I need but I don't want editors to create a sub-category. How can I remove/hide the "Parent" selectbox on the form?

Add bellow code in your current theme function.php file.
add_action( 'admin_head-edit-tags.php', 'wpse_58799_remove_parent_category' );
function wpse_58799_remove_parent_category()
{
if ( 'category' != $_GET['taxonomy'] )
return;
$parent = 'parent()';
if ( isset( $_GET['action'] ) )
$parent = 'parent().parent()';
?>
<script type="text/javascript">
jQuery(document).ready(function($)
{
$('label[for=parent]').<?php echo $parent; ?>.remove();
});
</script>
<?php
}

You can use set these options in
register_taxonomy() func
'hierarchical' => false,
'parent_item' => null,
'parent_item_colon' => null,
This would remove the parent field.

This will remove the parent dropdown from both the taxonomy and post new/edit screens.
<?php
function remove_tax_parent_dropdown() {
$screen = get_current_screen();
if ( 'category' == $screen->taxonomy ) {
if ( 'edit-tags' == $screen->base ) {
$parent = "$('label[for=parent]').parent()";
} elseif ( 'term' == $screen->base ) {
$parent = "$('label[for=parent]').parent().parent()";
}
} elseif ( 'post' == $screen->post_type ) {
$parent = "$('#newcategory_parent')";
} else {
return;
}
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
<?php echo $parent; ?>.remove();
});
</script>
<?php
}
add_action( 'admin_head-edit-tags.php', 'remove_tax_parent_dropdown' );
add_action( 'admin_head-term.php', 'remove_tax_parent_dropdown' );
add_action( 'admin_head-post.php', 'remove_tax_parent_dropdown' );
add_action( 'admin_head-post-new.php', 'remove_tax_parent_dropdown' );

If you want to disable "hierarchical" itself from category taxonomy, add this code in your function.php.
add_action('init', function(){
global $wp_taxonomies;
$wp_taxonomies['category']->hierarchical = false;
});

Add bellow code in your current theme function.php file.
function custom_taxonomy() {
$labels = array(
'name' => _x( 'Brands', 'Taxonomy General Name', 'text_domain' ),
'singular_name' => _x( 'Brand', 'Taxonomy Singular Name', 'text_domain' ),
'menu_name' => __( 'Taxonomy', 'text_domain' ),
'all_items' => __( 'All Items', 'text_domain' ),
'parent_item' => __( 'Parent Item', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'new_item_name' => __( 'New Item Name', 'text_domain' ),
'add_new_item' => __( 'Add New Item', 'text_domain' ),
'edit_item' => __( 'Edit Item', 'text_domain' ),
'update_item' => __( 'Update Item', 'text_domain' ),
'view_item' => __( 'View Item', 'text_domain' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
'add_or_remove_items' => __( 'Add or remove items', 'text_domain' ),
'choose_from_most_used' => __( 'Choose from the most used', 'text_domain' ),
'popular_items' => __( 'Popular Items', 'text_domain' ),
'search_items' => __( 'Search Items', 'text_domain' ),
'not_found' => __( 'Not Found', 'text_domain' ),
'no_terms' => __( 'No items', 'text_domain' ),
'items_list' => __( 'Items list', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'parent_item' => null,
'parent_item_colon' => null,
);
register_taxonomy( 'brands', array( 'product' ), $args );
}
add_action( 'init', 'custom_taxonomy', 0 );
Reference: https://codex.wordpress.org/Function_Reference/register_taxonomy

This will work with WordPress 5.4.2. For me all other solutions show the fields as long as jQuery removes them. My quick and dirty solution hides via CSS and remove them with jQuery. Unfortunately only hiding (not removing) seems to work with Gutenberg Editor. Maybe someone else has another solution.
function remove_tax_parent_dropdown() {
$screen = get_current_screen();
if ( 'category' == $screen->taxonomy ) {
if ( 'edit-tags' == $screen->base ) {
$parent = "$('label[for=parent]').parent().remove(); ";
$css = ".term-parent-wrap{display:none;}";
} elseif ( 'term' == $screen->base ) {
$parent = "$('label[for=parent]').parent().parent().remove(); ";
$css = ".term-parent-wrap{display:none;}";
}
} elseif ( 'post' == $screen->post_type ) {
$parent = "$('#newcategory_parent').remove();";
$css = "div.components-base-control:nth-child(3){display:none;}";
} else {
return;
}
if(!empty($css)) {
echo '<style type="text/css">';
echo $css;
echo '</style>';
}
if(!empty($parent)) {
echo '<script type="text/javascript">';
echo 'jQuery(document).ready(function($) {';
echo $parent;
echo '});';
echo '</script>';
}
}
add_action( 'admin_head-edit-tags.php', 'remove_tax_parent_dropdown' );
add_action( 'admin_head-term.php', 'remove_tax_parent_dropdown' );
add_action( 'admin_head-post.php', 'remove_tax_parent_dropdown' );
add_action( 'admin_head-post-new.php', 'remove_tax_parent_dropdown' );
Ah by the way - DON'T use the following code because you get bad issues with it. When you are saving a post without changing category, all categories of this post will be deleted and the category IDs will be created as new categories and added to your post.
global $wp_taxonomies;
$wp_taxonomies['category']->hierarchical = false;

Related

Custom post type taxonomy not being replaced in ACF post object field output

I have a custom post type (Photos) and a taxonomy (Locations) setup on my client's website. The permalinks work correctly when accessed using the menus, search, and directly from the custom post type in the WordPress admin panel, but not when accessing them using an Advanced Custom Fields post object field in my Timber/Twig template. The taxonomy portion (%locations%) of the URL is not being replaced. For example, http://example.com/photos/`%locations%`/taj-mahal-and-the-ganges/. The %locations% should be replaced with world and india, which are locations from the custom taxonomy.
The post's custom post object field is being pulled into the template using the following code: {{ __('View photo gallery', textdomain) }}.
I have included my custom post type and taxonomy code below:
function textdomain_register_photos_post_type() {
$args = [
'label' => 'Photo Galleries',
'labels' => [
'singular_name' => _x( 'Photo Gallery', 'singular' ),
'menu_name' => _x( 'Photo Galleries', 'admin menu' ),
'name_admin_bar' => _x( 'Photo Galleries', 'admin bar' ),
'add_new' => _x( 'Add New', 'add new' ),
'add_new_item' => __( 'Add New Photo Gallery' ),
'new_item' => __( 'New Photo Gallery' ),
'edit_item' => __( 'Edit Photo Gallery' ),
'view_item' => __( 'View Photo Gallery' ),
'all_items' => __( 'All Photo Galleries' ),
'search_items' => __( 'Search Photo Galleries' ),
'not_found' => __( 'No photo galleries found.' ),
],
'supports' => array(
'title',
'editor',
'thumbnail'
),
'public' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-format-gallery',
'capability_type' => 'post',
'taxonomies' => [ 'locations', ],
'has_archive' => true,
'delete_with_user' => false,
'rewrite' => [
'slug' => 'photos/%locations%',
'with_front' => false,
],
];
register_post_type( 'photos', $args );
};
add_action( 'init', 'textdomain_register_photos_post_type' );
function textdomain_register_locations_taxonomy() {
$args = [
'labels' => [
'name' => _x( 'Locations', 'taxonomy general name' ),
'singular_name' => _x( 'Location', 'taxonomy singular name' ),
'search_items' => __( 'Search Locations' ),
'all_items' => __( 'All Locations' ),
'parent_item' => __( 'Parent Location' ),
'parent_item_colon' => __( 'Parent Location:' ),
'edit_item' => __( 'Edit Location' ),
'update_item' => __( 'Update Location' ),
'add_new_item' => __( 'Add New Location' ),
'new_item_name' => __( 'New Location Name' ),
'menu_name' => __( 'Locations' ),
],
'hierarchical' => true,
'rewrite' => [
'slug' => 'locations',
'hierarchical' => true,
],
];
register_taxonomy( 'locations', [ 'photos' ], $args );
};
add_action( 'init', 'textdomain_register_locations_taxonomy' );
add_filter( 'post_type_link', 'textdomain_post_type_link', 10, 2 );
function textdomain_post_type_link( $post_link, $post ) {
// Bail out if not photos post type.
if ( 'photos' !== $post->post_type ) {
return $post_link;
}
$taxonomy = 'locations';
$terms = get_the_terms( get_the_ID(), $taxonomy );
$slug = [];
foreach ( $terms as $term ) {
if ( $term->parent == 0 ) {
array_unshift( $slug, sanitize_title_with_dashes( $term->name ) );
} else {
array_push( $slug, sanitize_title_with_dashes( $term->name ) );
}
}
if ( ! empty( $slug ) ) {
$post_link = str_replace( '%' . $taxonomy . '%', join( '/', $slug ), $post_link );
}
return $post_link;
}
I have saved my permalinks multiple times and have flush_rewrite_rules(); at the bottom of my theme's functions file.
Update
WordPress is displaying this warning Invalid argument supplied foreach() on line 422 of the functions.php file. The code is as follows:
$taxonomy = 'locations';
$terms = get_the_terms( get_the_ID(), $taxonomy );
$slug = [];
foreach ( $terms as $term ) {
if ( $term->parent == 0 ) {
array_unshift( $slug, sanitize_title_with_dashes( $term->name ) );
} else {
array_push( $slug, sanitize_title_with_dashes( $term->name ) );
}
}
I am not sure if this could be causing the issue, but my PHP knowledge is limited.
Any tips or suggestions on this issue are greatly appreciated.
I figured out the issue was related to how the post ID was being retrieved for the taxonomies. I reviewed the get_the_terms function reference and found that get_the_ID() needed to be replaced by $post->ID in the following line: $terms = get_the_terms( $post->ID, $taxonomy );. My updated and working function is below.
add_filter( 'post_type_link', 'textdomain_post_type_link', 10, 2 );
function textdomain_post_type_link( $post_link, $post ) {
// Bail out if not photos post type.
if ( 'photos' !== $post->post_type ) {
return $post_link;
}
$taxonomy = 'locations';
// Replaced get_the_ID() with $post->ID
$terms = get_the_terms( $post->ID, $taxonomy );
$slug = [];
foreach ( $terms as $term ) {
if ( $term->parent == 0 ) {
array_unshift( $slug, sanitize_title_with_dashes( $term->name ) );
} else {
array_push( $slug, sanitize_title_with_dashes( $term->name ) );
}
}
if ( ! empty( $slug ) ) {
$post_link = str_replace( '%' . $taxonomy . '%', join( '/', $slug ), $post_link );
}
return $post_link;
}

Wordpress get_the_terms for custom Taxonomy

Within an archive page, I'm trying to show the a custom taxonomy (called location) along with each post title, category and tag. I can't get the 'get_the_terms' to work.
The post title, category and tag is working perfectly. The taxonomy doesn't work.
<div class="post-listing">
<h4><?php the_title(); ?></h4>
<p><?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></p>
<p><?php $tags = get_the_tags(); foreach($tags as $tag) { echo "$tag->name"; } ?></p>
<p><?php $terms = get_the_terms( 'locations' ); foreach($terms as $term) { echo "$term->name"; } ?></p>
</div>
This is my functions.php code.
//hook into the init action and call create_locations_nonhierarchical_taxonomy when it fires
add_action( 'init', 'create_locations_nonhierarchical_taxonomy', 0 );
function create_locations_nonhierarchical_taxonomy() {
// Labels part for the GUI
$labels = array(
'name' => _x( 'Locations', 'taxonomy general name' ),
'singular_name' => _x( 'Location', 'taxonomy singular name' ),
'search_items' => __( 'Search Locations' ),
'popular_items' => __( 'Popular Locations' ),
'all_items' => __( 'All Locations' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Location' ),
'update_item' => __( 'Update Location' ),
'add_new_item' => __( 'Add New Location' ),
'new_item_name' => __( 'New Location Name' ),
'separate_items_with_commas' => __( 'Separate locations with commas' ),
'add_or_remove_items' => __( 'Add or remove locations' ),
'choose_from_most_used' => __( 'Choose from the most used locations' ),
'menu_name' => __( 'Locations' ),
);
// Now register the non-hierarchical taxonomy like tag
register_taxonomy('locations','post',array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'location' ),
));
}
Any ideas? It's driving me mad!
There are two parameters of get_the_terms function. They are $post and $taxonomy. Unlike the other "get_the_xxxx" functions that you can skip the $post (post object or post ID) parameter inside the loop, you must add the post object or post ID to the first parameter.
I think you should write
$terms = get_the_terms( $post, 'locations' );
Instead of
$terms = get_the_terms( 'locations' );
Documentation: https://developer.wordpress.org/reference/functions/get_the_terms/

How To Modify Show UI of a Registered Taxonomy On Demand

As you you know the 'show_ui' Boolean option in taking care of rendering or not rending the Taxonomy Menu on UI on registering ataxonomy.
function custom_taxonomy() {
$labels = array(
'name' => 'Taxonomies',
'singular_name' => 'Taxonomy',
'menu_name' => 'Taxonomy',
'all_items' => 'All Items',
'parent_item' => 'Parent Item',
'parent_item_colon' => 'Parent Item:',
'new_item_name' => 'New Item Name',
'add_new_item' => 'Add New Item',
'edit_item' => 'Edit Item',
'update_item' => 'Update Item',
'view_item' => 'View Item',
'separate_items_with_commas' => 'Separate items with commas',
'add_or_remove_items' => 'Add or remove items',
'choose_from_most_used' => 'Choose from the most used',
'popular_items' => 'Popular Items',
'search_items' => 'Search Items',
'not_found' => 'Not Found',
'no_terms' => 'No items',
'items_list' => 'Items list',
'items_list_navigation' => 'Items list navigation',
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'taxonomy', array( 'post' ), $args );
}
add_action( 'init', 'custom_taxonomy', 0 );
Is there any way to modify this option after registering the taxonomy? like any hook or filter to toggle the Boolean in functions.php
You can use register_taxonomy_args to filter the taxonomy options before the taxonomy is registered, or while it's being registered.
Example code:
/*
* #param array $args The taxonomy args such as show_ui.
* #param string $taxonomy The taxonomy name.
*/
add_filter( 'register_taxonomy_args', function ( $args, $taxonomy ) {
if ( 'my_taxonomy' === $taxonomy ) {
$args['show_ui'] = false;
}
return $args;
}, 10, 2 );
There is also an "action" you can "hook" into, where this filter is fired just after a taxonomy is registered. Here, you can, for example, assign other post/object type(s) to the taxonomy.
Example code:
/*
* #param string $taxonomy The taxonomy name.
*/
add_action( 'registered_taxonomy', function ( $taxonomy ) {
if ( 'my_taxonomy' === $taxonomy ) {
register_taxonomy_for_object_type( $taxonomy, 'post' );
}
} );
And if you must (or need to) modify the show_ui (or any other options) of a taxonomy after it's registered, then you can use the global $wp_taxonomies, which is an array of all registered taxonomies.
Example code:
/*
* #param string $taxonomy The taxonomy name.
*/
add_action( 'registered_taxonomy', function ( $taxonomy ) {
if ( 'my_taxonomy' === $taxonomy ) {
global $wp_taxonomies;
if ( ! is_array( $wp_taxonomies )
|| ! isset( $wp_taxonomies[ $taxonomy ] ) ) {
return;
}
$wp_taxonomies[ $taxonomy ]->show_ui = false;
}
} );

Doesn't work auto save custom fields in Wordpress

I'm using Smashing Magazine tutorial step by step. But there is doesn't work auto save in my custom fields. After click publish and update button the fields are empty. How to resolve the issue.
function my_custom_post_movie() {
$labels = array(
'name' => _x( 'movies', 'post type general name' ),
'singular_name' => _x( 'movie', 'post type singular name' ),
'add_new' => _x( 'Add New', 'book' ),
'add_new_item' => __( 'Add New movie' ),
'edit_item' => __( 'Edit movie' ),
'new_item' => __( 'New movie' ),
'all_items' => __( 'All movies' ),
'view_item' => __( 'View movie' ),
'search_items' => __( 'Search movies' ),
'not_found' => __( 'No movies found' ),
'not_found_in_trash' => __( 'No movies found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'movies'
);
$args = array(
'labels' => $labels,
'description' => 'Holds our movies and movie specific data',
'public' => true,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
'has_archive' => true,
);
register_post_type( 'movie', $args );
}
add_action( 'init', 'my_custom_post_movie' );
// Register Custom Taxonomies
function my_taxonomies_movie() {
$labels = array(
'name' => _x( 'movie Categories', 'taxonomy general name' ),
'singular_name' => _x( 'movie Category', 'taxonomy singular name' ),
'search_items' => __( 'Search movie Categories' ),
'all_items' => __( 'All movie Categories' ),
'parent_item' => __( 'Parent movie Category' ),
'parent_item_colon' => __( 'Parent movie Category:' ),
'edit_item' => __( 'Edit movie Category' ),
'update_item' => __( 'Update movie Category' ),
'add_new_item' => __( 'Add New movie Category' ),
'new_item_name' => __( 'New movie Category' ),
'menu_name' => __( 'movie Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
);
register_taxonomy( 'movie_category', 'movie', $args );
}
add_action( 'init', 'my_taxonomies_movie', 0 );
// Meta Box
add_action( 'add_meta_boxes', 'movie_date_box' );
function movie_date_box() {
add_meta_box(
'movie_date_box',
__( 'movie date', 'myplugin_textdomain' ),
'movie_date_box_content',
'movie',
'side',
'high'
);
}
function movie_date_box_content( $post ) {
wp_nonce_field( plugin_basename( __FILE__ ), 'movie_date_box_content_nonce' );
echo '<label for="movie_date"></label>';
echo '<input type="text" id="movie_date" name="movie_date" placeholder="enter a date" />';
}
add_action( 'save_post', 'movie_date_box_save' );
function movie_date_box_save( $post_id ) {
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( !wp_verify_nonce( $_POST['movie_date_box_content_nonce'], plugin_basename( __FILE__ ) ) )
return;
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) )
return;
} else {
if ( !current_user_can( 'edit_post', $post_id ) )
return;
}
$movie_date = $_POST['movie_date'];
update_post_meta( $post_id, 'movie_date', $movie_date );
}
Thanks for the help.
Your information is in fact being saved to the database, but it is not being called once the post has been submitted.
Change:
function movie_date_box_content( $post ) {
wp_nonce_field( plugin_basename( __FILE__ ), 'movie_date_box_content_nonce' );
echo '<label for="movie_date"></label>';
echo '<input type="text" id="movie_date" name="movie_date" placeholder="enter a date" />';
}
to the following:
function movie_date_box_content( $post ) {
wp_nonce_field( plugin_basename( __FILE__ ), 'movie_date_box_content_nonce' );
echo '<label for="movie_date"></label>';
echo '<input type="text" id="movie_date" name="movie_date" placeholder="enter a date" value="' . get_post_meta( $post->ID, 'movie_date', true ) . '" />';
}
You were missing value="' . get_post_meta( $post->ID, 'movie_date', true ) . '"
get_post_meta() calls the meta key movie_date from the database and shows it, if there is anything stored in that key.
Also change:
if ( !wp_verify_nonce( $_POST['movie_date_box_content_nonce'], plugin_basename( __FILE__ ) ) )
return;
to the following:
if ( !isset($_POST['movie_date_box_content_nonce']) || !wp_verify_nonce( $_POST['movie_date_box_content_nonce'], plugin_basename( __FILE__ ) ) )
return;
This checks to see if the nonce field is set and not NULL. It takes care of the Undeclared Index notice that shows up in the admin.

Showing custom taxonomy column in custom posts type listings

I have created a custom post type named banners. Thereby I register a new taxonomy called location that specifies on which page the banner is to be shown. Everything is great however when I click on the custom posts type 'Banner' in the admin window I see all the banners created however the table does not have a column for the taxonomy 'Location'.
In other words I want to be able to see what location the banner is in, in the banners listing.
For those interested, the register_taxonomy register_taxonomy function, as of WordPress 3.5, now offers an argument for show_admin_column (false by default). Set to true, it automatically displays the taxonomy column in the admin.
You can use the manage_post-type_custom_column and manage_edit_post-type_columns filters to add your taxonomy column to the post type listing.
add_action( 'admin_init', 'my_admin_init' );
function my_admin_init() {
add_filter( 'manage_edit-banner_columns', 'my_new_custom_post_column');
add_action( 'manage_banner_custom_column', 'location_tax_column_info', 10, 2);
}
function my_new_custom_post_column( $column ) {
$column['location'] = 'Location';
return $column;
}
function location_tax_column_info( $column_name, $post_id ) {
$taxonomy = $column_name;
$post_type = get_post_type($post_id);
$terms = get_the_terms($post_id, $taxonomy);
if (!empty($terms) ) {
foreach ( $terms as $term )
$post_terms[] ="<a href='edit.php?post_type={$post_type}&{$taxonomy}={$term->slug}'> " .esc_html(sanitize_term_field('name', $term->name, $term->term_id, $taxonomy, 'edit')) . "</a>";
echo join('', $post_terms );
}
else echo '<i>No Location Set. </i>';
}
//what version of wordpress you are using
//since wp 3.5 you can pass parameter show_admin_column=>true
// hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_book_taxonomies', 0 );
// create two taxonomies, genres and writers for the post type "book"
function create_book_taxonomies() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x( 'Genres', 'taxonomy general name' ),
'singular_name' => _x( 'Genre', 'taxonomy singular name' ),
'search_items' => __( 'Search Genres' ),
'all_items' => __( 'All Genres' ),
'parent_item' => __( 'Parent Genre' ),
'parent_item_colon' => __( 'Parent Genre:' ),
'edit_item' => __( 'Edit Genre' ),
'update_item' => __( 'Update Genre' ),
'add_new_item' => __( 'Add New Genre' ),
'new_item_name' => __( 'New Genre Name' ),
'menu_name' => __( 'Genre' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'genre' ),
);
register_taxonomy( 'genre', array( 'book' ), $args );
}
In addition to Jonathan Wold answer, you can add the ability to filter by your custom taxonomy like this:
add_action('restrict_manage_posts', 'filter_post_type_backend_by_taxonomies', 99, 2);
function filter_post_type_backend_by_taxonomies($post_type, $which) {
if( 'your_post_type' !== $post_type) {
return;
}
$taxonomies = array( 'your_custom_taxonomy' );
foreach( $taxonomies as $taxonomy_slug ) {
$taxonomy_obj = get_taxonomy( $taxonomy_slug );
$taxonomy_name = $taxonomy_obj->labels->name;
$terms = get_terms($taxonomy_slug);
echo "<select name='{$taxonomy_slug}' id='{$taxonomy_slug}' class='postform'>";
echo '<option value="">' . sprintf( esc_html__( 'Show all %s', 'domain-name' ), $taxonomy_name) . '</option>';
foreach ( $terms as $term ) {
printf(
'<option value="%1$s" %2$s>%3$s (%4$s)</option>',
$term->slug,
( ( isset( $_GET[$taxonomy_slug] ) && ( $_GET[$taxonomy_slug] == $term->slug ) ) ? ' selected="selected"' : '' ),
$term->name,
$term->count
);
}
echo '</select>';
}
}

Resources