Skip to content

Instantly share code, notes, and snippets.

@frasten
Created May 25, 2010 20:27
Show Gist options
  • Select an option

  • Save frasten/413642 to your computer and use it in GitHub Desktop.

Select an option

Save frasten/413642 to your computer and use it in GitHub Desktop.
--- lib/core.php 2010-05-25 20:41:16.136542548 +0200
+++ lib/core.php 2010-06-03 01:21:45.250096862 +0200
@@ -143,26 +143,15 @@
function start_el(&$output, $item, $depth, $args) {
global $wp_query;
$indent = ($depth) ? str_repeat("\t", $depth) : '';
- $classes = $value = '';
- $classes = array('type-'. $item->type, $item->classes);
+ $class_names = $value = '';
+ $classes = empty( $item->classes ) ? array() : (array) $item->classes;
- if ('custom' == $item->object){
- $current_url = (is_ssl() ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
- $item_url = strpos($item->url, '#') ? substr($item->url, 0, strpos($item->url, '#')) : $item->url;
- if ($item_url == $current_url) $classes[] = 'active';
- } else {
- $classes[] = 'object-'.$item->object;
- if ($item->object_id == $wp_query->get_queried_object_id()) $classes[] = 'active';
- }
-
- // @todo add classes for parent/child relationships
-
- $classes = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item));
- $classes = ' class="' . esc_attr($classes) . '"';
+ $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
+ $class_names = ' class="' . esc_attr( $class_names ) . '"';
- $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $classes .'>';
+ $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
$attributes = ! empty($item->attr_title) ? ' title="' . esc_attr($item->attr_title) .'"' : '';
$attributes .= ! empty($item->target) ? ' target="' . esc_attr($item->target) .'"' : '';
@@ -170,10 +159,10 @@
$attributes .= ! empty($item->url) ? ' href="' . esc_attr($item->url) .'"' : '';
$item_output .= '<a'. $attributes .' class="fadeThis">';
- $item_output .= '<span class="title">' . apply_filters('the_title', $item->title) . '</span>';
+ $item_output .= '<span class="title">' . apply_filters('the_title', $item->title, $item->ID) . '</span>';
$item_output .= '<span class="pointer"></span></a>';
- $output .= apply_filters('wp_get_nav_menu_item', $item_output, $args);
+ $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
function end_el(&$output, $item, $depth) {
@@ -205,6 +194,7 @@
$items = '';
$sorted_menu_items = array();
foreach ((array) $menu_items as $key => $menu_item) $sorted_menu_items[$menu_item->menu_order] = wp_setup_nav_menu_item($menu_item);
+ _mystique_menu_item_classes_by_context( $sorted_menu_items );
$walker = new mystique_MenuWalker();
$items .= $walker->walk($sorted_menu_items, 0, array());
@@ -275,6 +265,133 @@
<?php endif;
}
+/**
+ * Add the class property classes for the current frontend context, if applicable.
+ *
+ * @access private
+ * @since 3.0
+ *
+ * @param array $menu_items The current menu item objects to which to add the class property information.
+ */
+function _mystique_menu_item_classes_by_context( &$menu_items ) {
+ global $wp_query;
+
+ $queried_object = $wp_query->get_queried_object();
+ $queried_object_id = (int) $wp_query->queried_object_id;
+
+ $active_object = '';
+ $active_parent_item_ids = array();
+ $active_parent_object_ids = array();
+ $possible_object_parents = array();
+ $home_page_id = (int) get_option( 'page_for_posts' );
+
+ if ( $wp_query->is_singular && ! empty( $queried_object->post_type ) && ! is_post_type_hierarchical( $queried_object->post_type ) ) {
+ foreach ( (array) get_object_taxonomies( $queried_object->post_type ) as $taxonomy ) {
+ if ( is_taxonomy_hierarchical( $taxonomy ) ) {
+ $terms = wp_get_object_terms( $queried_object_id, $taxonomy, array( 'fields' => 'ids' ) );
+ if ( is_array( $terms ) )
+ $possible_object_parents = array_merge( $possible_object_parents, $terms );
+ }
+ }
+ } elseif ( ! empty( $queried_object->post_type ) && is_post_type_hierarchical( $queried_object->post_type ) ) {
+ _get_post_ancestors( $queried_object );
+ }
+
+ $possible_object_parents = array_filter( $possible_object_parents );
+
+ foreach ( (array) $menu_items as $key => $menu_item ) {
+ $classes = (array) $menu_item->classes;
+ $classes[] = 'menu-item';
+ $classes[] = 'menu-item-type-' . $menu_item->type;
+
+ // if the menu item corresponds to a taxonomy term for the currently-queried non-hierarchical post object
+ if ( $wp_query->is_singular && 'taxonomy' == $menu_item->type && in_array( $menu_item->object_id, $possible_object_parents ) ) {
+ $active_parent_object_ids[] = (int) $menu_item->object_id;
+ $active_parent_item_ids[] = (int) $menu_item->db_id;
+ $active_object = $queried_object->post_type;
+
+ // if the menu item corresponds to the currently-queried post or taxonomy object
+ } elseif (
+ $menu_item->object_id == $queried_object_id &&
+ (
+ ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && $wp_query->is_home && $home_page_id == $menu_item->object_id ) ||
+ ( 'post_type' == $menu_item->type && $wp_query->is_singular ) ||
+ ( 'taxonomy' == $menu_item->type && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ) )
+ )
+ ) {
+ $classes[] = 'active';
+ if ( 'post_type' == $menu_item->type && 'page' == $menu_item->object ) {
+ // Back compat classes for pages to match wp_page_menu()
+ $classes[] = 'page_item';
+ $classes[] = 'page-item-' . $menu_item->object_id;
+ $classes[] = 'current_page_item';
+ }
+ $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
+ $active_parent_object_ids[] = (int) $menu_item->post_parent;
+ $active_object = $menu_item->object;
+
+ // if the menu item corresponds to the currently-requested URL
+ } elseif ( 'custom' == $menu_item->object ) {
+ $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
+ $item_url = strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url;
+ if ( $item_url == $current_url ) {
+ $classes[] = 'active';
+ if ( untrailingslashit($current_url) == home_url() ) {
+ $classes[] = 'home';
+ // Back compat for home limk to match wp_page_menu()
+ $classes[] = 'current_page_item';
+ }
+ $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
+ $active_parent_object_ids[] = (int) $menu_item->post_parent;
+ $active_object = $menu_item->object;
+ }
+ }
+
+ // back-compat with wp_page_menu: add "current_page_parent" to static home page link for any non-page query
+ if ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && empty( $wp_query->is_page ) && $home_page_id == $menu_item->object_id )
+ $classes[] = 'current_page_parent';
+
+ $menu_items[$key]->classes = array_unique( $classes );
+ }
+
+ $active_parent_item_ids = array_filter( array_unique( $active_parent_item_ids ) );
+ $active_parent_object_ids = array_filter( array_unique( $active_parent_object_ids ) );
+
+ // set parent's class
+ // TODO: @Milenko: please choose the CSS classes you want to use.
+ // Please see the original patch here:
+ // http://core.trac.wordpress.org/changeset/14876
+ foreach ( (array) $menu_items as $key => $parent_item ) {
+ $classes = (array) $parent_item->classes;
+
+ if (
+ isset( $parent_item->type ) &&
+ 'post_type' == $parent_item->type &&
+ ! empty( $queried_object->post_type ) &&
+ is_post_type_hierarchical( $queried_object->post_type ) &&
+ in_array( $parent_item->object_id, $queried_object->ancestors )
+ ) {
+ $classes[] = 'current-' . $queried_object->post_type . '-ancestor';
+ $classes[] = 'active-ancestor';
+ }
+ if ( in_array( $parent_item->db_id, $active_parent_item_ids ) )
+ $classes[] = 'active-parent';
+ if ( in_array( $parent_item->object_id, $active_parent_object_ids ) )
+ $classes[] = 'current-' . $active_object . '-parent';
+
+ if ( 'post_type' == $parent_item->type && 'page' == $parent_item->object ) {
+ // Back compat classes for pages to match wp_page_menu()
+ if ( in_array('active-parent', $classes) )
+ $classes[] = 'current_page_parent';
+ if ( in_array('active-ancestor', $classes) )
+ $classes[] = 'current_page_ancestor';
+ }
+
+ $menu_items[$key]->classes = array_unique( $classes );
+ }
+}
+
+
// based on hybrid theme's title
function mystique_title($separator = ' &laquo; '){
global $wp_query;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment