Skip to content

Commit

Permalink
Update navwalker and add itemprop=name to menù
Browse files Browse the repository at this point in the history
  • Loading branch information
overclokk committed Jun 17, 2014
1 parent 964bf7f commit 21e57e8
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 65 deletions.
22 changes: 12 additions & 10 deletions header.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@
<meta itemprop="image" content="<?php echo italystrap_logo();?>"/>
</span>
</div>
<?php wp_nav_menu(
array(
'menu' => 'main-menu',
'container_class' => 'navbar-collapse collapse',
'menu_class' => 'nav navbar-nav',
'fallback_cb' => '',
'menu_id' => 'main-menu',
'walker' => new wp_bootstrap_navwalker()
)
); ?>
<?php wp_nav_menu(
array(
'menu' => 'main-menu',
'depth' => 2,
'container' => 'div',
'container_class' => 'navbar-collapse collapse',
'menu_class' => 'nav navbar-nav',
'fallback_cb' => 'wp_bootstrap_navwalker::fallback',
'menu_id' => 'main-menu',
'walker' => new wp_bootstrap_navwalker()
)
); ?>
</div>
</div>
</div>
Expand Down
168 changes: 114 additions & 54 deletions lib/wp_bootstrap_navwalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@
/**
* Class Name: wp_bootstrap_navwalker
* GitHub URI: https://github.com/twittem/wp-bootstrap-navwalker
* Description: A custom WordPress nav walker class to implement the Twitter Bootstrap 2.3.2 navigation style in a custom theme using the WordPress built in menu manager.
* Version: 1.4.3
* Description: A custom WordPress nav walker class to implement the Bootstrap 3 navigation style in a custom theme using the WordPress built in menu manager.
* Version: 2.0.4
* Author: Edward McIntyre - @twittem
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/

class wp_bootstrap_navwalker extends Walker_Nav_Menu {

/**
* @see Walker::start_lvl()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of page. Used for padding.
*/
function start_lvl(&$output, $depth = 0, $args = Array() ) {
public function start_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat( "\t", $depth );
$output .= "\n$indent<ul class=\"dropdown-menu\">\n";
$output .= "\n$indent<ul role=\"menu\" class=\" dropdown-menu\">\n";
}

/**
Expand All @@ -34,71 +34,87 @@ function start_lvl(&$output, $depth = 0, $args = Array() ) {
* @param int $current_page Menu item ID.
* @param object $args
*/

function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
global $wp_query;
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

/**
* Dividers & Headers
* ==================
* Determine whether the item is a Divider, Header, or regular menu item.
* To prevent errors we use the strcasecmp() function to so a comparison
* that is not case sensitive. The strcasecmp() function returns a 0 if
* the strings are equal.
* Dividers, Headers or Disabled
* =============================
* Determine whether the item is a Divider, Header, Disabled or regular
* menu item. To prevent errors we use the strcasecmp() function to so a
* comparison that is not case sensitive. The strcasecmp() function returns
* a 0 if the strings are equal.
*/
if (strcasecmp($item->title, 'divider') == 0) {
// Item is a Divider
$output .= $indent . '<li class="divider">';
} else if (strcasecmp($item->title, 'divider-vertical') == 0) {
// Item is a Vertical Divider
$output .= $indent . '<li class="divider-vertical">';
} else if (strcasecmp($item->title, 'nav-header') == 0) {
// Item is a Header
$output .= $indent . '<li class="nav-header">' . esc_attr( $item->attr_title );
if ( strcasecmp( $item->attr_title, 'divider' ) == 0 && $depth === 1 ) {
$output .= $indent . '<li role="presentation" class="divider">';
} else if ( strcasecmp( $item->title, 'divider') == 0 && $depth === 1 ) {
$output .= $indent . '<li role="presentation" class="divider">';
} else if ( strcasecmp( $item->attr_title, 'dropdown-header') == 0 && $depth === 1 ) {
$output .= $indent . '<li role="presentation" class="dropdown-header">' . esc_attr( $item->title );
} else if ( strcasecmp($item->attr_title, 'disabled' ) == 0 ) {
$output .= $indent . '<li role="presentation" class="disabled"><a href="#">' . esc_attr( $item->title ) . '</a>';
} else {

$class_names = $value = '';

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

$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );

if ($args->has_children && $depth > 0) {
$class_names .= ' dropdown-submenu';
} else if($args->has_children && $depth === 0) {
if ( $args->has_children )
$class_names .= ' dropdown';
}

if ( in_array( 'current-menu-item', $classes ) )
$class_names .= ' active';

$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

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

$output .= $indent . '<li' . $id . $value . $class_names .'>';
$output .= $indent . '<li' . $id . $value . $class_names .' itemprop="name">';

$atts = array();
$atts['title'] = ! empty( $item->title ) ? $item->title : '';
$atts['target'] = ! empty( $item->target ) ? $item->target : '';
$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';

// If item has_children add atts to a.
if ( $args->has_children && $depth === 0 ) {
$atts['href'] = '#';
$atts['data-toggle'] = 'dropdown';
$atts['class'] = 'dropdown-toggle';
} else {
$atts['href'] = ! empty( $item->url ) ? $item->url : '';
}

$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 ) .'"' : '';
$attributes .= ($args->has_children) ? ' data-toggle="dropdown" data-target="#" class="dropdown-toggle"' : '';
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );

$attributes = '';
foreach ( $atts as $attr => $value ) {
if ( ! empty( $value ) ) {
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
$attributes .= ' ' . $attr . '="' . $value . '"';
}
}

$item_output = $args->before;
/**

/*
* Glyphicons
* ===========
* Since the the menu item is NOT a Divider or Header we check the see
* if there is a value in the attr_title property. If the attr_title
* property is NOT null we apply it as the class name for the glyphicon.
*/
if(! empty( $item->attr_title )){
$item_output .= '<a'. $attributes .' itemprop="url"><i class="' . esc_attr( $item->attr_title ) . '"></i>&nbsp;';
} else {
$item_output .= '<a'. $attributes .' itemprop="url">';
}

if ( ! empty( $item->attr_title ) )
$item_output .= '<a'. $attributes .' itemprop="url"><span class="glyphicon ' . esc_attr( $item->attr_title ) . '"></span>&nbsp;';
else
$item_output .= '<a'. $attributes .' itemprop="url">';

$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$item_output .= ($args->has_children && $depth == 0) ? ' <span class="caret"></span></a>' : '</a>';
$item_output .= ( $args->has_children && 0 === $depth ) ? ' <span class="caret"></span></a>' : '</a>';
$item_output .= $args->after;

$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
Expand All @@ -110,7 +126,7 @@ function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
*
* Display one element if the element doesn't have any children otherwise,
* display the element and its children. Will only traverse up to the max
* depth and no ignore elements under that depth.
* depth and no ignore elements under that depth.
*
* This method shouldn't be called directly, use the walk() method instead.
*
Expand All @@ -125,21 +141,65 @@ function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
* @param string $output Passed by reference. Used to append additional content.
* @return null Null on failure with no changes to parameters.
*/

function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
if ( !$element ) {
public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
if ( ! $element )
return;
}

$id_field = $this->db_fields['id'];

//display this element
if ( is_object( $args[0] ) ) {
$args[0]->has_children = ! empty( $children_elements[$element->$id_field] );
}
// Display this element.
if ( is_object( $args[0] ) )
$args[0]->has_children = ! empty( $children_elements[ $element->$id_field ] );

parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
}
}

?>
/**
* Menu Fallback
* =============
* If this function is assigned to the wp_nav_menu's fallback_cb variable
* and a manu has not been assigned to the theme location in the WordPress
* menu manager the function with display nothing to a non-logged in user,
* and will add a link to the WordPress menu manager if logged in as an admin.
*
* @param array $args passed from the wp_nav_menu function.
*
*/
public static function fallback( $args ) {
if ( current_user_can( 'manage_options' ) ) {

extract( $args );

$fb_output = null;

if ( $container ) {
$fb_output = '<' . $container;

if ( $container_id )
$fb_output .= ' id="' . $container_id . '"';

if ( $container_class )
$fb_output .= ' class="' . $container_class . '"';

$fb_output .= '>';
}

$fb_output .= '<ul';

if ( $menu_id )
$fb_output .= ' id="' . $menu_id . '"';

if ( $menu_class )
$fb_output .= ' class="' . $menu_class . '"';

$fb_output .= '>';
$fb_output .= '<li><a href="' . admin_url( 'nav-menus.php' ) . '">Add a menu</a></li>';
$fb_output .= '</ul>';

if ( $container )
$fb_output .= '</' . $container . '>';

echo $fb_output;
}
}
}
2 changes: 1 addition & 1 deletion style.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Theme URI: http://www.overclokk.net/italystrap-wordpress-starter-theme
Author: Enea Overclokk
Author URI: http://www.overclokk.net
Description: Wordpess starter theme with twitter bootstrap, HTML5 boilerplate and Schema.org <a href="https://github.com/overclokk/ItalyStrap/graphs/contributors">Contribute on GitHub</a>
Version: 1.7.0
Version: 1.7.1
License: MIT License (MIT)
License URI: http://opensource.org/licenses/MIT
Tags: right-sidebar, featured-images, full-width-template, microdata
Expand Down

0 comments on commit 21e57e8

Please sign in to comment.