Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php /*
- Plugin Name: Add Children As Menu Items
- Work in progress! Mostly a mockup at this point. -Alex
- */
- //add_filter( 'wp_nav_menu_objects', 'filter_var_dump', 99 );
- function filter_var_dump( $var ) {
- var_dump( $var );
- return $var;
- }
- class Add_Children_As_Menu_Items {
- public $added = array();
- function __construct() {
- if ( ! is_admin() ) {
- add_filter( 'wp_get_nav_menu_items', array( &$this, 'add_child_pages_to_menu' ), 10, 3 );
- }
- }
- // Add all decendent pages to top level menu items that are pages
- function add_child_pages_to_menu( $items, $menu, $args ) {
- $menu_order = count( $items ) + 1000;
- $filter_added = false;
- foreach ( $items as $item ) {
- // Skip menu items that aren't top level or aren't post_type=page
- if ( 0 != $item->menu_item_parent || 'page' != $item->object )
- continue;
- // Get all decendent pages
- $children = get_pages( array(
- 'child_of' => $item->object_id,
- 'sort_column' => 'menu_order',
- ) );
- if ( ! $children )
- continue;
- // Menu items are being added, so later fix the "current" values for highlighting
- if ( ! $filter_added )
- add_filter( 'wp_nav_menu_objects', array( &$this, 'fix_menu_current_item' ) );
- // Add each child page to the menu
- foreach ( $children as $child ) {
- $this->added[$child->ID] = true; // We'll need this later
- $child = wp_setup_nav_menu_item( $child );
- $child->db_id = $child->ID;
- // Set the parent menu item.
- // If the current page is a child of the current menu item,
- // then use that menu item's ID otherwise use the real ID
- // because it's a menu item that this plugin added itself.
- if ( $child->post_parent == $item->object_id )
- $child->menu_item_parent = $item->ID; // Children
- else
- $child->menu_item_parent = $child->post_parent; // Grandchildren, etc.
- // The menu_order has to be unique, so make up new ones
- // The items are already sorted anyway due to the get_posts()
- $menu_order++;
- $child->menu_order = $menu_order;
- $items[] = $child;
- }
- }
- //var_dump( $items );
- return $items;
- }
- // Fixes the ancestor/parent settings/classes when viewing a menu item added by this plugin
- function fix_menu_current_item( $items ) {
- global $wp_query;
- $queried_object = $wp_query->get_queried_object();
- $queried_object_id = (int) $wp_query->queried_object_id;
- // Only need to fix items added by this plugin
- if ( empty( $this->added[$queried_object_id] ) )
- return $items;
- _get_post_ancestors( $queried_object );
- foreach ( $items as $item ) {
- if ( ! in_array( $item->object_id, $queried_object->ancestors ) )
- continue;
- $item->current_item_ancestor = true;
- $item->classes[] = 'current-menu-ancestor';
- $item->classes[] = 'current_page_ancestor';
- // If menu item is direct parent of current page
- if ( $item->object_id == $queried_object->post_parent ) {
- $item->current_item_parent = true;
- $item->classes[] = 'current-menu-parent';
- $item->classes[] = 'current_page_parent';
- }
- }
- return $items;
- }
- }
- $add_children_as_menu_items = new Add_Children_As_Menu_Items();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement