I am customizing a plugin. I needed it to show a list of the categories in hierarchy, which I got working with all of the functions of the plugin. I’m using wp_dropdown_categories
, but I’d like to display the list of categories as their slugs, and not as their category names. Any suggestions?
Here’s what I have so far:
function replace_id_for_slug( $option ) {
$categories = get_categories( "hide_empty=0" );
preg_match( '/value="(d*)"/', $option[0], $matches );
$id = $matches[1];
$selectID = $nextItem;
$slug = get_cat_slug( $id );
foreach ( $categories as $category ) {
if ( $category->cat_ID == $id ) {
}
}
return preg_replace( "/value="(d*)"/", "value="$slug"", $option[0] );
}
$select = wp_dropdown_categories(
"hierarchical=1&hide_empty=0&echo=0&name=field_$nextItem&id=$selectID&class=categoriesBox"
);
echo $select;
I forgot to mention that this is for the admin backend. I’ve tried playing around with the walker class, but I am at a loss. Do you know of any other way this could be accomplished?
There is an argument
walker
forwp_dropdown_categories()
. It accepts an instance of a custom walker, a class extendingWalker_CategoryDropdown
or the genericWalker
.Letâs create such a class. We have to change just one method.
Now we create an instance of our class â¦
⦠and pass it to the dropdown:
Note, this is completely untested, just an idea to show you the direction. 🙂
I did by a different way and works fine. Hope it can help too:
Here is an updated version of answer provided by @toscho, tested and working with WP 4.1.1.