Skip to content

Instantly share code, notes, and snippets.

@mattboon
Last active December 12, 2015 06:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattboon/4731680 to your computer and use it in GitHub Desktop.
Save mattboon/4731680 to your computer and use it in GitHub Desktop.
<?php
// Return the page id by slug
// -------------------------------------------------------------
function get_ID_by_slug( $page_slug ) {
$page = get_page_by_path( $page_slug );
if ( $page ) {
return $page->ID;
} else {
return null;
}
}
// Return if we're in the Projects section
// -------------------------------------------------------------
function is_projects() {
return ( is_singular( 'project' ) || is_post_type_archive( 'project' ) );
}
// Return the root parent ID of a given post
// -------------------------------------------------------------
function get_root_parent_id( $post_id = null ) {
if ( ! $post_id ) {
global $post;
$post_id = $post->ID;
}
global $wpdb;
$parent = $wpdb->get_var( "SELECT post_parent FROM $wpdb->posts WHERE post_type='page' AND post_status='publish' AND ID = '$post_id'" );
if ( $parent == 0 ) return $post_id;
else return get_root_parent_id( $parent );
}
// Remove wp_nav_menu() containers
// -------------------------------------------------------------
function my_wp_nav_menu_args( $args = '' ) {
$args['container'] = false;
return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
// Remove all wp_nav_menu() classes (and add .is-current)
// -------------------------------------------------------------
function my_wp_nav_strip_classes( $a ){
return ( in_array( 'current_page_item', $a ) ) ? array( 'is-current' ) : array();
}
add_filter( 'nav_menu_css_class', 'my_wp_nav_strip_classes', 10, 2 );
// Add special classes to wp_nav_menu
// -------------------------------------------------------------
function my_wp_nav_special_classes( $classes, $item ){
if( $item->object_id == get_root_parent_id() || $item->object_id == get_ID_by_slug( 'project' ) && is_projects() ) {
$classes[] = 'is-root-parent';
}
return $classes;
}
add_filter( 'nav_menu_css_class' , 'my_wp_nav_special_classes', 10, 2 );
// Remove wp_nav_menu() IDs
// -------------------------------------------------------------
function my_wp_nav_strip_id() {
return '';
}
add_filter( 'nav_menu_item_id', 'my_wp_nav_strip_id' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment