Created
August 6, 2014 14:57
-
-
Save remcotolsma/366824ca488f9207ff34 to your computer and use it in GitHub Desktop.
WordPress menu walker classes for custom post types and archives.
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 | |
/** | |
* Walker | |
* | |
* @see http://codex.wordpress.org/Class_Reference/Walker | |
* @see https://github.com/WordPress/WordPress/blob/3.9.1/wp-includes/nav-menu-template.php#L351-L358 | |
*/ | |
function prefix_nav_menu_objects( $sorted_menu_items ) { | |
$walker = new Pronamic_WP_Walker_Nav_Menu_Classes(); | |
$walker->walk( $sorted_menu_items, 0 ); | |
return $sorted_menu_items; | |
} | |
add_filter( 'wp_nav_menu_objects', 'prefix_nav_menu_objects' ); | |
/** | |
* Walker | |
*/ | |
class Pronamic_WP_Walker_Nav_Menu_Classes extends Walker_Nav_Menu { | |
private $archive_url; | |
private $elements; | |
public function __construct() { | |
$this->elements = array(); | |
$post_type = get_query_var( 'post_type' ); | |
$this->is_singular = is_singular(); | |
$this->is_archive = is_post_type_archive(); | |
$this->archive_url = get_post_type_archive_link( $post_type ); | |
} | |
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { | |
if ( $this->archive_url == $item->url ) { | |
if ( $this->is_archive ) { | |
$item->classes[] = 'prefix-current-archive'; | |
} | |
if ( $this->is_singular ) { | |
$item->classes[] = 'prefix-current-archive-ancestor'; | |
} | |
$this->active[ $depth ] = true; | |
} | |
} | |
function end_el( &$output, $item, $depth = 0, $args = array() ) { | |
$depth_child = $depth + 1; | |
if ( isset( $this->active[ $depth_child ] ) ) { | |
$item->classes[] = 'prefix-current-custom-ancestor'; | |
$this->active[ $depth ] = true; | |
unset( $this->active[ $depth_child ] ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment