Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Sliding doors menu for use with WP Custom menus WordPress

Just asking this to save time. Used to add spans and use preg replace. However I want this to work with WP3.1.2 custom menus.

I would like the css for a sliding doors technique for buttons. Usually I use a left image and a right image. I would prefer to not hack the menu building code. One menu will have these buttons and other menus will be completely different so I don't want to affect them.

forgot to add - this will be a horizontal menu.

Answers (1)

2011-05-05

Ryan Riatno answers:

Try to use custom walker in you menu. Put this in your theme functions.php
class custom_walker extends Walker_Nav_Menu
{
function start_el(&$output, $item, $depth, $args) {
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

$class_names = $value = '';

$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;

$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
$class_names = ' class="' . esc_attr( $class_names ) . '"';

$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
$id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';

$output .= $indent . '<li' . $id . $value . $class_names .'><span></span>';

$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';

$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;

$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}

}


wp_nav_menu( array(
'container' =>false,
'menu_class' => 'nav',
'echo' => true,
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'depth' => 0,
'walker' => new custom_walker())
);


And you can work with the css something like this :
.nav li span {
background:url(images/menu-left.png) no-repeat left;
}


Basically it would output :

<ul class="menu-main">
<li><span></span><a href="#">Home</a></li>
</ul>

Notice the "<span></span>" after <li>


Connie Taylor comments:

does this go into the functions as well? not sure if it was a continuation of the other code box:
wp_nav_menu( array(
'container' =>false,
'menu_class' => 'nav',
'echo' => true,
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'depth' => 0,
'walker' => new custom_walker())
);


Connie Taylor comments:

I get this error after adding it to my functions

<blockquote>Fatal error: Cannot redeclare class custom_walker in /home2/brandmag/public_html/lakota/wp-content/themes/lakota/functions.php on line 99
</blockquote>


Connie Taylor comments:

found my error in the above but I must be doing something wrong.

This is putting a new menu in - not affecting the existing wp custom menus. In addition the new menu is appearing before the header doctype code.


Ryan Riatno comments:

Yes, it goes to "functions.php". Try to rename the class to something else like "sliding_door_custom_walker"


Connie Taylor comments:

This is only adding a "new" menu at the top of the code <strong>above</strong> the html and head tags. The new menu has the span tags but it does not affect the existing menu in the register menus area.

I will have to search for another option.


Connie Taylor comments:

I found my own answer. Using
wp_nav_menu( array( 'link_before' => '<span>', 'link_after' => '</span>'
Then it was easy to style.