Skip to content

Instantly share code, notes, and snippets.

@nmyers
Last active March 6, 2018 11:09
Show Gist options
  • Save nmyers/bf96f586eec2a2ac2f143f1f323094c4 to your computer and use it in GitHub Desktop.
Save nmyers/bf96f586eec2a2ac2f143f1f323094c4 to your computer and use it in GitHub Desktop.
Wordpress menu with icons #wp
<?php
class Svg_Menu_Walker extends Walker_Nav_Menu {
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
$t = '';
$n = '';
} else {
$t = "\t";
$n = "\n";
}
$indent = ( $depth ) ? str_repeat( $t, $depth ) : '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;
$args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
$output .= $indent . '<li' . $id . $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, $depth );
$attributes = '';
foreach ( $atts as $attr => $value ) {
if ( ! empty( $value ) ) {
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
$attributes .= ' ' . $attr . '="' . $value . '"';
}
}
/** This filter is documented in wp-includes/post-template.php */
$title = apply_filters( 'the_title', $item->title, $item->ID );
$title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= svg_icon(sanitize_title($title));
$item_output .= '<span>' . $title . '</span>';
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
}
- if (isset($menu) || isset($location)):
nav.menu-icons(role='navigation',class!=$class)
- wp_nav_menu(array('menu'=>$menu,'theme_location'=>$location,'container'=>false,'walker'=>new Svg_Menu_Walker))
- endif
.menu-icons {
&.grid-4, &.grid-5, &.grid-6, &.grid-7 {
// @extend %l-ml50;
ul {
display: flex;
flex-flow: row wrap;
// justify-content: flex-start;
}
li {
@extend %l-p50;
flex: 1 1 1;
width: 25%;
display: block;
background-clip: content-box;
text-align: center;
@include bp-s {
width: 33.33%;
}
}
span {
@extend %t-sub;
}
.icon {
@extend %l-mt50,%l-mb50;
}
}
&.grid-5 {
@extend %l-mb200;
@include bp-l {
width: 110%;
}
li{
width: 20%;
@include bp-s {
width: 33.33%;
}
}
}
&.grid-6 {
@extend %l-mb200;
@include bp-l {
width: 110%;
}
li{
width: 16.66%;
@include bp-s {
width: 33.33%;
}
}
}
&.grid-7 {
@extend %l-mb200;
@include bp-l {
width: 110%;
}
li{
width: 14.28%;
@include bp-s {
width: 33.33%;
}
}
}
a {
display: block;
color: #000;
}
span {
display: block;
}
.icon {
width: 100%;
height: auto;
max-width: 9rem;
max-height: 8.5rem;
padding: 0.5em;
}
.current-menu-item {
&:before , &:after {
content: '';
display: block;
position: absolute;
height: 1px;
border-top: 1px solid #000;
top: 0;
width: 80%;
left: 10%;
}
&:after {
top: auto;
bottom: 0;
}
}
.current-menu-item a , a:hover{
color: $color_b;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment