Add a divider between specific menu items
-
Good day,
I am wondering how to add a
<hr>divider between menu items.I have been trying in a child theme in includes/theme-menu.php, with no success.
I am trying to get a custom url menu item with url “#” and the label “Divider” to appear as a styled
<hr />vice the label name.Here is the modified section of theme-menu.php, was wondering if someone could figure out what I’m doing wrong:
class Graphene_Description_Walker extends Walker_Nav_Menu {
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
global $wp_query;
$indent = ( $depth ) ? str_repeat( "t", $depth ) : '';
$class_names = $value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
$class_names = ' class="'. esc_attr( $class_names ) . '"';
/* ### Added following section ### */
if ( 'Divider' === $item->title )
{
// you may remove the <hr> here and use plain CSS.
//$output .= '<li class="menu_separator"><hr>';
$output .= '<li><hr style="margin: 0; border: 0px solid; height: 2px; background: #333; width: 200px; ">';
}
else
{
/* #################### */
$output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
/* ### Added following section ### */
}
/* #################### */and
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
$item_output .= $description.$args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
/* #################### */
/* ### Added following section ### */
if ( 'Divider' === $item->title )
{
// you may remove the <hr> here and use plain CSS.
//$output .= '<li class="menu_separator"><hr>';
$output .= '<li><hr style="margin: 0; border: 0px solid; height: 2px; background: #333; width: 200px; ">';
//$output .= '';
}
else
{
/* #################### */
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
/* ### Added following section ### */
}
/* #################### */
}
}My menu still outputs an item labelled as “Divider” vice a
<hr />divider. Any idea what I’m doing wrong? It used to work.Dave
I figured it out myself.
My modifications were in a child theme’s ./includes/theme-menu.php, which was never getting loaded.
For anyone interested, I used the original ./includes/theme-menu.php “
class Graphene_Description_Walker extends Walker_Nav_Menu {” as a template for my own modified “class Custom_Description_Walker extends Walker_Nav_Menu {“, and then a custom header.php to call “$args = array_merge( $args, array( 'walker' => new Custom_Description_Walker() ) );” at line 123.I did not include any of the remainder of the original theme-menu.php in my custom theme-menu.php, as it is loaded with the original (parent) theme.
I also had to include “
require( dirname( __FILE__ ) . '/includes/theme-menu.php' ); // Functions for navigation menus” in my child theme’s function.php file.Dave
Viewing 2 posts - 1 through 2 (of 2 total)
- You must be logged in to reply to this topic.
