Last active
August 29, 2015 14:07
-
-
Save jhned/c197e4c4c3c7c30cc091 to your computer and use it in GitHub Desktop.
Walker for Custom Post Type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
class Custom_Display_Children_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 = ''; | |
$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>'; | |
$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 . $class_names .'>'; | |
/** This filter is documented in wp-includes/post-template.php */ | |
if (strpos($item->classes[0],'icon') !== false) { | |
$item_output .= cnp_isvg('icon-name='. $item->classes[0] .'&echo=0'); | |
} | |
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after; | |
$item_output .= '</a>'; | |
if ( $item->type == 'post_type_archive' && $item->title == 'Case Studies' ) { | |
$post_type_entries = get_posts( 'post_type=custom-post-type&numberposts=-1&orderby=menu_order&order=ASC' ); | |
if (empty($post_type_entries)) | |
return; | |
$children = ''; | |
foreach ($post_type_entries as $key => $entry) { | |
$class_names = ''; | |
if ($wp_query->query_vars['name'] == $entry->post_name) | |
$class_names .= 'class="current-menu-item"'; | |
$children .= '<li><a '. $class_names .' href="'. get_permalink($entry->ID) .'" >'. $entry->post_title .'</a></li>'; | |
} | |
if (empty($children)) | |
return; | |
$item_output .= '<ul class="children">'. $children .'</ul>'; | |
} | |
$item_output .= $args->after; | |
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment