Created
November 29, 2018 15:53
-
-
Save VitalyKondratiev/049c64650aed483400e2cbf20d11eae8 to your computer and use it in GitHub Desktop.
Wordpress taxonomy menu (wp_get_nav_menu_items hook)
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
add_filter("wp_get_nav_menu_items", function ($items, $menu, $args) { | |
$menu_id = 21; // Wordpress Menu ID | |
$menu_item_id = 48; // Wordpress Menu Item ID | |
$taxonomy_name = 'product_cat'; // Wordpress Taxonomy Code | |
$max_depth = 3; // Maximum level of menu depth | |
if( $menu->term_id != $menu_id || is_admin()) return $items; | |
$get_nav_from_terms = function (&$items, $terms, $parent_menu_id, $order, $depth = 1) use ($max_depth, &$get_nav_from_terms) { | |
if ($depth > $max_depth) return; | |
foreach ($terms as $term) { | |
$item = new stdClass(); | |
$item->ID = 1000000 + $order + $parent_menu_id; | |
$item->db_id = $item->ID; | |
$item->title = $term->name; | |
$item->url = get_term_link($term); | |
$item->menu_order = $order++; | |
$item->menu_item_parent = $parent_menu_id; | |
$items[] = $item; | |
$terms_child = get_terms(array('taxonomy' => 'product_cat', 'parent' => $term->term_id )); | |
if (!empty($terms_child)) { | |
$get_nav_from_terms($items, $terms_child, $item->ID, $order, $depth + 1); | |
} | |
} | |
}; | |
$terms = get_terms(array('taxonomy' => $taxonomy_name, 'parent' => 0)); | |
$get_nav_from_terms($items, $terms, $menu_item_id, 100); | |
return $items; | |
}, 10, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment