Skip to content

Instantly share code, notes, and snippets.

@sterndata
Created August 20, 2017 19:41
Show Gist options
  • Save sterndata/2f3dafbf054088709320e04b65cf833b to your computer and use it in GitHub Desktop.
Save sterndata/2f3dafbf054088709320e04b65cf833b to your computer and use it in GitHub Desktop.
function tachp_make_department_menu( $items, $args ) {
global $wp;
if ( 'main' != $args->menu->slug ) {
return $items;
}
$terms = get_terms('Department', array(
'orderby' => 'name',
'hide_empty' => true,
)
);
// what is the current page?
$current_url = add_query_arg( array(), $wp->request );
ob_start();
?>
<li id="menu-item-dept-list" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-dept-list>"><a href="#">By Department</a>
<?php
echo '<ul class="sub-menu">';
foreach ( $terms as $a ) {
?>
<?php
if ( 'departments/' . $a->slug == $current_url ) {
$current_classes = ' current-menu-item ';
} else {
$current_classes = '';
}
?>
<li id="menu-item-dept-<?php echo $a->slug;
?>" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-dept-<?php echo $a->slug; echo $current_classes;
?>"><a href="/departments/<?php echo $a->slug;
?>/"><?php echo $a->name;
?></a></li>
<?php
}
echo '</li></ul>';
$contents = ob_get_contents();
/*
* find the first occurrence of "All Classes"
* and put this list right after_title
*/
$search = 'All Classes</a></li>';
$replace = $search . $contents;
$newstr = str_replace( $search, $replace, $items );
ob_end_clean();
return $newstr;
}
add_action( 'wp_nav_menu_items', 'tachp_make_department_menu', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment