dragunoff

wpquestions.com, ID = 3170

Jan 15th, 2012
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.69 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * set the ID attribute of nav menu items
  5.  */
  6. add_filter( 'nav_menu_item_id', 'pagenamed_id', 10, 2 );
  7.  
  8. function pagenamed_id( $id, $item ) {
  9.  
  10.     if ( $content_page_id = get_post_meta ( $item->ID, '_menu_item_object_id', true ) ) {
  11.         if ( $content_page = get_post ( $content_page_id ) )
  12.             $id = $content_page->post_name;
  13.     }
  14.  
  15.     return $id;
  16.  
  17. }
  18.  
  19. /**
  20.  * set classes on nav menu item
  21.  *
  22.  * @note contains a large portion of the function _wp_menu_item_classes_by_context()
  23.  */
  24. add_filter( 'nav_menu_css_class', 'filter_nav_menu_classes', 10, 2 );
  25.  
  26. function filter_nav_menu_classes( $classes, $menu_item ) {
  27.    
  28.     $classes = array();
  29.  
  30.     global $wp_query;
  31.  
  32.     $queried_object = $wp_query->get_queried_object();
  33.     $queried_object_id = (int) $wp_query->queried_object_id;
  34.  
  35.     $active_object = '';
  36.     $active_ancestor_item_ids = array();
  37.     $active_parent_item_ids = array();
  38.     $active_parent_object_ids = array();
  39.     $possible_taxonomy_ancestors = array();
  40.     $possible_object_parents = array();
  41.     $home_page_id = (int) get_option( 'page_for_posts' );
  42.  
  43.     if ( $wp_query->is_singular && ! empty( $queried_object->post_type ) && ! is_post_type_hierarchical( $queried_object->post_type ) ) {
  44.         foreach ( (array) get_object_taxonomies( $queried_object->post_type ) as $taxonomy ) {
  45.             if ( is_taxonomy_hierarchical( $taxonomy ) ) {
  46.                 $term_hierarchy = _get_term_hierarchy( $taxonomy );
  47.                 $terms = wp_get_object_terms( $queried_object_id, $taxonomy, array( 'fields' => 'ids' ) );
  48.                 if ( is_array( $terms ) ) {
  49.                     $possible_object_parents = array_merge( $possible_object_parents, $terms );
  50.                     $term_to_ancestor = array();
  51.                     foreach ( (array) $term_hierarchy as $anc => $descs ) {
  52.                         foreach ( (array) $descs as $desc )
  53.                             $term_to_ancestor[ $desc ] = $anc;
  54.                     }
  55.  
  56.                     foreach ( $terms as $desc ) {
  57.                         do {
  58.                             $possible_taxonomy_ancestors[ $taxonomy ][] = $desc;
  59.                             if ( isset( $term_to_ancestor[ $desc ] ) ) {
  60.                                 $_desc = $term_to_ancestor[ $desc ];
  61.                                 unset( $term_to_ancestor[ $desc ] );
  62.                                 $desc = $_desc;
  63.                             } else {
  64.                                 $desc = 0;
  65.                             }
  66.                         } while ( ! empty( $desc ) );
  67.                     }
  68.                 }
  69.             }
  70.         }
  71.     } elseif ( ! empty( $queried_object->post_type ) && is_post_type_hierarchical( $queried_object->post_type ) ) {
  72.         _get_post_ancestors( $queried_object );
  73.     } elseif ( ! empty( $queried_object->taxonomy ) && is_taxonomy_hierarchical( $queried_object->taxonomy ) ) {
  74.         $term_hierarchy = _get_term_hierarchy( $queried_object->taxonomy );
  75.         $term_to_ancestor = array();
  76.         foreach ( (array) $term_hierarchy as $anc => $descs ) {
  77.             foreach ( (array) $descs as $desc )
  78.                 $term_to_ancestor[ $desc ] = $anc;
  79.         }
  80.         $desc = $queried_object->term_id;
  81.         do {
  82.             $possible_taxonomy_ancestors[ $queried_object->taxonomy ][] = $desc;
  83.             if ( isset( $term_to_ancestor[ $desc ] ) ) {
  84.                 $_desc = $term_to_ancestor[ $desc ];
  85.                 unset( $term_to_ancestor[ $desc ] );
  86.                 $desc = $_desc;
  87.             } else {
  88.                 $desc = 0;
  89.             }
  90.         } while ( ! empty( $desc ) );
  91.     }
  92.  
  93.     $possible_object_parents = array_filter( $possible_object_parents );
  94.  
  95.     $front_page_url = home_url();
  96.    
  97.     // if the menu item corresponds to the currently-queried post or taxonomy object
  98.     if (
  99.         $menu_item->object_id == $queried_object_id &&
  100.         (
  101.             ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && $wp_query->is_home && $home_page_id == $menu_item->object_id ) ||
  102.             ( 'post_type' == $menu_item->type && $wp_query->is_singular ) ||
  103.             ( 'taxonomy' == $menu_item->type && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ) )
  104.         )
  105.     ) {
  106.         $classes[] = 'current-menu-item';
  107.         // $menu_items[$key]->current = true;
  108.         $_anc_id = (int) $menu_item->db_id;
  109.  
  110.         while(
  111.             ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
  112.             ! in_array( $_anc_id, $active_ancestor_item_ids )
  113.         ) {
  114.             $active_ancestor_item_ids[] = $_anc_id;
  115.         }
  116.  
  117.         $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
  118.         $active_parent_object_ids[] = (int) $menu_item->post_parent;
  119.         $active_object = $menu_item->object;
  120.  
  121.         // if the menu item corresponds to the currently-requested URL
  122.         } elseif ( 'custom' == $menu_item->object ) {
  123.             $current_url = untrailingslashit( ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
  124.             $item_url = untrailingslashit( strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url );
  125.             $_indexless_current = untrailingslashit( preg_replace( '/index.php$/', '', $current_url ) );
  126.  
  127.             if ( in_array( $item_url, array( $current_url, $_indexless_current ) ) ) {
  128.                 $classes[] = 'current-menu-item';
  129.                 // $menu_items[$key]->current = true;
  130.                 $_anc_id = (int) $menu_item->db_id;
  131.  
  132.                 while(
  133.                     ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
  134.                     ! in_array( $_anc_id, $active_ancestor_item_ids )
  135.                 ) {
  136.                     $active_ancestor_item_ids[] = $_anc_id;
  137.                 }
  138.  
  139.                 if ( in_array( home_url(), array( untrailingslashit( $current_url ), untrailingslashit( $_indexless_current ) ) ) ) {
  140.                     // Back compat for home link to match wp_page_menu()
  141.                     $classes[] = 'current_page_item';
  142.                 }
  143.                 $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
  144.                 $active_parent_object_ids[] = (int) $menu_item->post_parent;
  145.                 $active_object = $menu_item->object;
  146.  
  147.             // give front page item current-menu-item class when extra query arguments involved
  148.             } elseif ( $item_url == $front_page_url && is_front_page() ) {
  149.                 $classes[] = 'current-menu-item';
  150.             }
  151.  
  152.             if ( untrailingslashit($item_url) == home_url() )
  153.                 $classes[] = 'menu-item-home';
  154.         }
  155.        
  156.     // return the new array with classes
  157.     return $classes;
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment