Skip to content

Instantly share code, notes, and snippets.

@wturnerharris
Last active November 25, 2021 05:41
Show Gist options
  • Save wturnerharris/9278526 to your computer and use it in GitHub Desktop.
Save wturnerharris/9278526 to your computer and use it in GitHub Desktop.
Use custom walker to modify wp_nav_menu()
<?php
/* **** USAGE ****
* <?php wp_nav_menu( array(
* 'walker' => new custom_walker_nav_menu()
* )); ?>
*/
class custom_walker_nav_menu extends Walker {
var $tree_type = array( 'post_type', 'taxonomy', 'custom' );
var $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
function start_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth+1);
$output .= "\t<span class=\"icon icon-arrow-down\"></span>\n$indent\t<ul class=\"sub-menu\">\n";
}
function end_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth+1);
$output .= "$indent\t</ul>\n";
}
function start_el(&$output, $item, $depth, $args) {
global $wp_query;
$products = array( 'siris', 'siris-lite', 'alto', 'genisis', 'g-series' );
$replace = array(' '=> '-');
$shortened_title = strtolower(strtr($item->title, $replace));
$indent = ( $depth ) ? str_repeat( "\t", $depth+1 ) : '';
$class_names = $value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes = in_array( 'current-menu-item', $classes ) ? array( 'current-menu-item' ) : $classes;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
if (in_array($shortened_title, $products) && $depth==1){
$class_names .= ' product ' . $shortened_title;
} else if ($depth==1) {
$class_names .= ' page';
}
$class_names = strlen( trim( $class_names ) ) > 0 ? ' class="' . esc_attr( $class_names ) . '"' : '';
$id = apply_filters( 'nav_menu_item_id', '', $item, $args );
$id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
$output .= "\n$indent\t" .'<li' . $id . $value . $class_names .'>'."\n\t$indent\t";
$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;
if (in_array($shortened_title, $products) && $depth==1){
$img_url = wp_get_attachment_image_src(get_post_thumbnail_id($item->object_id), 'medium');
$img_src = $img_url[0];
$item_output .= "<br><img src=\"$img_src\" alt=\"$shortened_title\" />";
} else if ($depth==1) {
$item_output .= ' <strong>&gt;</strong>';
}
$item_output .= "</a>\n$indent\t";
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
function end_el(&$output, $item, $depth) {
$output .= "</li>\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment