Last active
August 29, 2015 14:02
-
-
Save markgoodyear/40040810ad8ac0d1d089 to your computer and use it in GitHub Desktop.
WordPress Dirty Active Menu
This file contains hidden or 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
<?php | |
add_filter( 'nav_menu_css_class', 'add_parent_url_menu_class', 10, 2 ); | |
function add_parent_url_menu_class( $classes = array(), $item = false ) { | |
// Get current URL | |
$current_url = current_url(); | |
// Get homepage URL | |
$homepage_url = trailingslashit(get_bloginfo('url')); | |
// Exclude 404 and homepage | |
if(is_404() || $item->url == $homepage_url) return $classes; | |
if (strstr( $current_url, $item->url)) { | |
// Add the 'parent_url' class | |
$classes[] = 'parent_url'; | |
} | |
return $classes; | |
} | |
function current_url() { | |
// Protocol | |
$url = 'http://'; | |
$url .= $_SERVER['SERVER_NAME']; | |
// Port | |
$url .= ('80' == $_SERVER['SERVER_PORT']) ? '' : ':' . $_SERVER['SERVER_PORT']; | |
$url .= $_SERVER['REQUEST_URI']; | |
return trailingslashit($url); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment