wp_nav_menu, walker class, categories as classes of li

OK I’m trying to show my principal menu so I use the following code:

<?php $defaults = array(
    'theme_location'  => '',
    'menu'            => '', 
    'container'       => false,
    'container_id'    => '',
    'menu_class'      => 'menu', 
    'menu_id'         => '',
    'echo'            => true,
    'fallback_cb'     => 'wp_page_menu',
    'before'          => '',
    'after'           => '',
    'link_before'     => '',
    'link_after'      => '',
    'items_wrap'      => '<ul id="%1$s" class="unstyled pull-right %2$s">%3$s</ul>',
    'depth'           => 0,
    'walker'          => new description_walker()
); ?>
<?php wp_nav_menu( $defaults ); ?>

The menu is filled with the category names (not pages) and it outputs the following:

Read More
<ul id="menu-principal" class="unstyled pull-right menu">
<li id="menu-item-597" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-597"><a href="#">menu</a></li>
<li id="menu-item-595" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-595"><a href="#">menu</a></li>
<li id="menu-item-596" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-596"><a href="#">menu</a></li>
<li id="menu-item-593" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-593"><a href="#">menu</a></li>
<li id="menu-item-594" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-594"><a href="#">menu</a></li>
<li id="menu-item-592" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-592"><a href="#">menu</a></li>
<li id="menu-item-591" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-591"><a href="#">menu</a></li>
<li id="menu-item-592" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-592"><a href="#">menu</a></li>
<li id="menu-item-591" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-591"><a href="#">menu</a></li>
</ul>

I would to replace all these li classes with the category name so I try to use a walker class in functions.php: https://gist.github.com/1053467

So I have minimal stuff but I don’t know how to add the classes. class=”category-categoryname” (i.e. class=”category-photography”).

Many thanks for your time and help.

Related posts

Leave a Reply

1 comment

  1. your walker class is almost perfect for what you need @Gab.

    instead of line 21:

            $output     .= '<li>';
    

    move it down just before line 38

    $output .= apply_filters(
            'walker_nav_menu_start_el' [...]
    

    and change it to something like

    $output .= $indent . '<li id="nav-menu-item-'. $item->ID . '" class="'. $title . '">';
    

    and you should have the category name as the first class of your li’s

    You see, because you’ve built the nice title $title you can use it to build the li tag, and then build the a tag as you had before.


    edit:

     $mycatid = $item->object_id;
     $category = get_category($mycatid );
     $mycatslug = $category->slug;
     $output .= $indent . '<li id="nav-menu-item-'. $item->ID . '" class="'. $mycatslug . '">';