1

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:

<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.

1 Answer 1

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 . '">';
3
  • It works but the problem is that the title has an Uppercase. It would be great I could really catch the category name (slug).
    – Gab
    Jul 27, 2012 at 5:47
  • well that's a bit more tricky, because $item doesn't have the slug. it does however have the id. with it you can $mycatid = $item->object_id; $category = get_category($mycatid ); $mycatslug = $category->slug;
    – pcarvalho
    Jul 27, 2012 at 16:08
  • glad i could be of help! :)
    – pcarvalho
    Jul 27, 2012 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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