Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This might be a newb question, but I'm not sure. I did wuite a bit of searching, but i can't find anything to answer my question... 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 name. Any suggestions?

Here's what I have so far:

<?php
            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;

?>  

Hi Toscho, thanks for the reply. I forgot to mention in my question 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?

Thanks, Chase

share|improve this question
    
Where does $nextItem come from? –  kaiser Jan 31 '13 at 14:03
    
$nextItem relates to the field name used by the plugin I was modifying. –  Michael Chase Mar 8 '13 at 0:44

1 Answer 1

up vote 0 down vote accepted

There is an argument walker for wp_dropdown_categories(). It accepts an instance of a custom walker, a class extending Walker_CategoryDropdown or the generic Walker.

Let’s create such a class. We have to change just one method.

class WPSE_Cat_Slug_Walker extends Walker_Category
{   function start_el( &$output, $category, $depth, $args, $id = 0 ) {
        $pad = str_repeat('&nbsp;', $depth * 3);

        $output .= "\t<option class=\"level-$depth\" value=\"".$category->term_id."\"";
        if ( $category->term_id == $args['selected'] )
            $output .= ' selected="selected"';
        $output .= '>';
        $output .= $pad.$category->slug; // The Slug!
        if ( $args['show_count'] )
            $output .= '&nbsp;&nbsp;('. $category->count .')';
        $output .= "</option>\n";
    }
}

Now we create an instance of our class …

$wpse_cat_slug_walker = new WPSE_Cat_Slug_Walker;

… and pass it to the dropdown:

$select = wp_dropdown_categories(
    array (
        'hierarchical' => 1,
        'hide_empty'   => 0,
        'echo'         => 0,
        'name'         => "field_$nextItem",
        'id'           => $selectID,
        'class'        => 'categoriesBox',
        'walker'       => $wpse_cat_slug_walker // the walker
    )
);

Note, this is completely untested, just an idea to show you the direction. :)

share|improve this answer
    
Thanks for your help toscho! I actually ended up going a different direction with it, basically just forgetting about using the slugs for now. I appreciate your response though. :) –  Michael Chase Dec 12 '12 at 18:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.