On Github jaffe75 / WordCamp-Denver
Bridging the gap between Microsoft Technologies and the Open Source community
Web Camps TV gets you closer to the Microsoft Web Platform with exclusive interviews and demos from the guys that make it easy for you to create rock star web applications
Men's Health Awareness Month
What is it, and why should I give s#!t?
Who has heard of it? Who has used it? Don't let them ask questions.. cause they are smarter then you.The Walker class was implemented in WordPress 2.1 to provide developers with a means to traverse tree-like data structures for the purpose of rendering HTML.
We're mostly going to focus on Navigation Menu's, but...
function walk ($elements, $max_depth)
We start with the walk method, this is the method that actually returns the HTML once it has been created. It takes two arguments..
Display_Element
This function is responsible for displaying an element in the tree
function display_element($element, &$children_elements, $max_depth, $depth=0, $args &$output){}Basically this is saying, traverse all of the items and if there are children then let someone know about it.
let's go over each thingy-ma-jigger
I will be showing code that is used in Walker_Nav_Menu
function start_lvl(&$output, $depth=0, $args=array())
This is the start of the sub-level items
Most commonly wrapped in a <ul> tag for navigation purposes
can also be wrapped in div or ol tags, as they are common as well.function start_lvl( &$output, $depth = 0, $args = array() ) { $indent = str_repeat("\t", $depth); $output .= '\n$indent<ul class="sub-menu">\n'; }
function end_lvl(&$output, $depth=0, $args=array())
This is the end of the sub-level items
Most commonly uses the </ul> tag for navigation purposes
can also be closing ol or div tagsfunction end_lvl(&$output, $depth=0, array()){ $indent = str_repeat("\t", $depth); $output .= "$indent</ul>\n"; }
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 )
This is the start of the element wrapper -
most commonly a <li> tag
this can include <a> and span tags(WTF = Why The Feelings?), so many feelings...
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { $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_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 .'>'; $atts = array(); $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : ''; $atts['target'] = ! empty( $item->target ) ? $item->target : ''; $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : ''; $atts['href'] = ! empty( $item->url ) ? $item->url : ''; $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; $item_output .= '<a'. $attributes .'>'; /** This filter is documented in wp-includes/post-template.php */ $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after; $item_output .= '</a>'; $item_output .= $args->after;
ugly cats says SLOW DOWN...
This just indents our code so it looks pretty when viewing source..
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
creating 2 variables, setting the classes...
$class_names = $value = ''; $classes = empty( $item->classes ) ? array() : (array) $item->classes; $classes[] = 'menu-item-' . $item->ID;
The CSS applied to to a menu's item in this case <li>
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) ); $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';NEXT>>>
The ID that is applied to the <li>
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args ); $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
And we do a little outputting ... notice the $indent variable.. huh?
$output .= $indent . '<li' . $id . $value . $class_names .'>';
Checking to see if the attributes have content, if not then...
$atts = array(); $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : ''; $atts['target'] = ! empty( $item->target ) ? $item->target : ''; $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : ''; $atts['href'] = ! empty( $item->url ) ? $item->url : '';NEXT>>>
Filtering through the attributes for the <a> tag
$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 . '"'; } }
Outputing our arguments and attributes before, middle, and after..
$item_output = $args->before; $item_output .= '<a'. $attributes="" .'="">'; /** This filter is documented in wp-includes/post-template.php */ $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after; $item_output .= ''; $item_output .= $args->after;</a'.>
and finally
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );$item_output The menu item's starting HTML output. $item Menu item data object. $depth Depth of menu item. Used for padding. $args An array of arguments. @see wp_nav_menu()
function end_el( &$output, $item, $depth = 0, $args = array() )
we close out the <li> tag
function end_el( &$output, $args = array() ){ $output .= "</li>\n"; }
insert large amounts of fanfair here
Foundation_s is a starter theme that is being created by combining Foundation by Zurb and _s by Automattic*
*they do WordPress stuff.. pass it on..This is actually what made me decide to put this as a base class in foundation_s