Show main parent category when active child Resolved Requested: 1 year and 7 months ago How to show main category when user have active child page (when use, Advnced Sidebar – Navigation or Advanced Sidebar Pages): Cat1 — Sub1 — Sub2 (active) — Sub3 Cat2 (show) — Sub21 (hide) — Sub22 (hide) — Sub23 (hide) Cat3 (show)
OnPoint Plugins 1 year and 7 months ago Hi Ćukasz, The “Advanced Sidebar – Pages” block is not intended to show categories so that one is probably not feasible. Using “Advanced Sidebar – Categories” You may use the Categories block by adding this filter to your active theme’s functions.php file. ‘Always display child categories’ must be unchecked. <?php /** * Limit the category menu items to the current family and top level items * when on a child item. * * Same as "Display the current item's section only" (not an option for categories) except: * - Also includes top level items. * - Has no effect when viewing a top level item. * - Has no effect when not viewing a menu item. * * @paramOption Widget must have 'Always display child categories' unchecked. * * @ticket #13167 */ use Advanced_Sidebar_Menu\Menus\Category; add_filter( 'advanced-sidebar-menu/menus/category/get-child-terms', function( array $terms, Category $class ) { if ( null === $class->get_current_term() || 0 === $class->get_current_term()->parent ) { return $terms; } return \array_filter( $terms, function( $term ) use ( $class ) { return $class->get_highest_parent( $term->term_id ) === $class->get_highest_parent( $class->get_current_term()->term_id ); } ); }, 10, 2 ); /** * Include all top level categories in the menu when on a child item. * * - Has no effect when viewing a top level item. * - Has no effect when not viewing a menu item. * * @paramOption Widget must have 'Always display child categories' unchecked. * * @ticket #13167 */ add_filter( 'advanced-sidebar-menu/menus/category/top-level-term-ids', function( array $term_ids, array $args, array $instance, Category $class ) { if ( null === $class->get_current_term() || 0 === $class->get_current_term()->parent ) { return $term_ids; } $terms = get_terms( [ 'hide_empty' => false, 'parent' => 0, 'fields' => 'ids', ] ); return \array_filter( $terms, function( $term_id ) use ( $class ) { return ! $class->is_excluded( $term_id ); } ); }, 10, 4 ); Using “Advanced Sidebar – Navigation” You may use the Navigation block by adding this filter to the your active theme’s functions.php file. ‘Display the current item’s section only’ must be unchecked. /** * Limit the navigation menu items to the current family and top level items * when on a child item. * * Same as "Display the current item's section only" except * - Also includes top level items. * - Has no effect when viewing a top level item. * - Has no effect when not viewing a menu item. * * @paramOption Widget must have "Display the current item's section only" unchecked. * * @ticket #13167 */ use Advanced_Sidebar_Menu\Nav_Menu\Objects; add_action( 'advanced-sidebar-menu/menus/navigation/objects/parse-hierarchy', function( array $hierarchy, $current, Objects $objects ): void { if ( false === $current || 0 === $current->menu_item_parent ) { return; } foreach ( $objects->menu_items as $id => $item ) { if ( 0 !== (int) $item->menu_item_parent && ! $objects->is_in_current_family( $item->get_id() ) ) { unset( $objects->menu_items[ $id ] ); } } }, 10, 3 ); Either option will have the same effect: Only affect the menu when viewing a child or grandchild item. Only display top level items and the current item’s direct family. Have a great day!
Hi Ćukasz,
The “Advanced Sidebar – Pages” block is not intended to show categories so that one is probably not feasible.
Using “Advanced Sidebar – Categories”
You may use the Categories block by adding this filter to your active theme’s functions.php file.
Using “Advanced Sidebar – Navigation”
You may use the Navigation block by adding this filter to the your active theme’s functions.php file.
Either option will have the same effect:
Have a great day!