I have been away of wordpress for quite a while and never practiced that much. I have a few problems even tho I looked on the internet for an answer... regarding the wp_nav_menu behaviour.
Hope you guys will be able to help and understand my english.
The problem is the following:
Short version: I'm trying to rewrite the way <li>
will be outputted from the PHP in the HTML. Right now, it's the default way of Wordpress to output the navigation. There is a nav -> ul -> li -> a
. I am trying with the filter to rework the li to have something like <li><a href="#' . $url . '"><span>' . $title . '</span><i class="fa ' . $list_ico_class . '"/></a></li>
You can see this last line at the end of the filter :)
Long version: I have a primary navigation which display the categories of a custom post type "projects" (doing a new version of my portfolio). I would have probably 3 categories: Design, Frontend, Applications. The menu items would then be those 3 categories + a blog link.
I am trying to add a custom class for each list item depending on the title of the item. After looking on the internet, the solution seemed to be to create a function that would filter the content.
I wrote the following based on different sources:
add_filter( 'wp_nav_menu_items', 'my_custom_menu_item', 10, 2 );
function my_custom_menu_item ( $items, $args ) {
if (is_single() && $args->theme_location == 'primary') {
foreach ( (array) $menu_items as $key => $menu_item ) {
$title = $menu_item->title;
$url = $menu_item->url;
if ($title == 'Design') {
$list_ico_class = 'fa-pencil';
}
else if ($title == 'Frontend') {
$list_ico_class = 'fa-desktop';
}
else if ($title == 'Applications') {
$list_ico_class = 'fa-code';
}
else if ($title == 'Blog') {
$list_ico_class = 'fa-user';
}
else {
$list_ico_class = 'fa-question';
}
$menu_list .= '<li><a href="#' . $url . '"><span>' . $title . '</span><i class="fa ' . $list_ico_class . '"/></a></li>';
}
}
return $menu_items;
}
Then, in my header.php template, I did the menu in a "regular" way since I am not sure how to proceed..
<nav class="site-navigation main-navigation">
<span class="nav-info">Smoothly go to<i class="fa fa-long-arrow-right"></i></span>
<?php
$main_nav_values = array(
'theme_location' => 'primary',
'menu' => '',
'container' => 'false',
'container_class' => '',
'container_id' => '',
'menu_class' => 'main-navigation', 'site-navigation',
'menu_id' => 'primary-menu',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul id="%1$s">%3$s</ul>',
'depth' => 0,
'walker' => ''
);
wp_nav_menu($main_nav_values);
?>
</nav> <!-- .site-navigation .main-navigation -->
The menu doesn't appear anymore unfortunately when the filter function is present... Not triggering any error, probably just a null object ?
I tried following what's suggested here: http://codex.wordpress.org/Plugin_API/Filter_Reference/nav_menu_css_class and there Get menu item slug
The desired output would be the following:
<nav class="site-navigation main-navigation">
<span class="nav-info">Smoothly go to<i class="fa fa-long-arrow-right"></i></span>
<ul>
<li><a href=""><span>Design</span><i class="fa fa-pencil"></i></a></li>
<li><a href=""><span>Frontend development</span><i class="fa fa-desktop"></i></a></li>
<li><a href=""><span>Application & Games</span><i class="fa fa-code"></i></a></li>
<li><a href=""><span>Blog</span><i class="fa fa-user"></i></a></li>
</ul>
</nav> <!-- .site-navigation .main-navigation -->
Any idea what would be my mistake ? thanks in advance, feel free to ask if more details are needed!
$menu_items
but that variable is never set.$item
but honestly say m not clear on what you are trying to do.<li>
will be outputted from the PHP in the HTML. Right now, it's the default way of Wordpress to output the navigation. There is anav -> ul -> li -> a
. I am trying with the filter to rework theli
to have something like<li><a href="#' . $url . '"><span>' . $title . '</span><i class="fa ' . $list_ico_class . '"/></a></li>
You can see this last line at the end of the filter :) Is that better explained ? ahah