I have created a menu under Appearance > menus called "Sidebar Menu Collection"
This is their heirarchy
Material
- marble
- onyx
- slate
- granite
Applications
- benchtops
- floors
- walls
products
- Slab
- pavers
- cladding
So when I am on the page "Material" I would like to have a list of its child menu (marble,onyx,slate,grantite) only.
If I'm in "Applications" page it would show (benctops,floors,walls). And so on, I would like to add a featured image on every child list based on the page featured image.
Also this will be applicable only to the parent menu (Material,Applications,Products) and if your on the sub-menu no list will be shown
Hello use below code will only show childs of specific pages
wp_list_pages( $args = '' ) {
$defaults = array(
'depth' => 1,
'show_date' => '',
'date_format' => get_option( 'date_format' ),
'child_of' => $post->ID,
'exclude' => '',
'title_li' => __( 'Pages' ),
'echo' => 1,
'authors' => '',
'sort_column' => 'menu_order, post_title',
'link_before' => '',
'link_after' => '',
'item_spacing' => 'preserve',
'walker' => '',
);
Already had an answer to this, came from this post.
This is working perfectly, the only problem I have now is how to add a featured image on every link.
// add hook
add_filter( 'wp_nav_menu_objects', 'my_wp_nav_menu_objects_sub_menu', 10, 2 );
// filter_hook function to react on sub_menu flag
function my_wp_nav_menu_objects_sub_menu( $sorted_menu_items, $args ) {
if ( isset( $args->sub_menu ) ) {
$root_id = 0;
// find the current menu item
foreach ( $sorted_menu_items as $menu_item ) {
if ( $menu_item->current ) {
// set the root id based on whether the current menu item has a parent or not
$root_id = ( $menu_item->menu_item_parent ) ? $menu_item->menu_item_parent : $menu_item->ID;
break;
}
}
// find the top level parent
if ( ! isset( $args->direct_parent ) ) {
$prev_root_id = $root_id;
while ( $prev_root_id != 0 ) {
foreach ( $sorted_menu_items as $menu_item ) {
if ( $menu_item->ID == $prev_root_id ) {
$prev_root_id = $menu_item->menu_item_parent;
// don't set the root_id to 0 if we've reached the top of the menu
if ( $prev_root_id != 0 ) $root_id = $menu_item->menu_item_parent;
break;
}
}
}
}
$menu_item_parents = array();
foreach ( $sorted_menu_items as $key => $item ) {
// init menu_item_parents
if ( $item->ID == $root_id ) $menu_item_parents[] = $item->ID;
if ( in_array( $item->menu_item_parent, $menu_item_parents ) ) {
// part of sub-tree: keep!
$menu_item_parents[] = $item->ID;
} else if ( ! ( isset( $args->show_parent ) && in_array( $item->ID, $menu_item_parents ) ) ) {
// not part of sub-tree: away with it!
unset( $sorted_menu_items[$key] );
}
}
return $sorted_menu_items;
} else {
return $sorted_menu_items;
}
}
Usage
wp_nav_menu( array(
'menu' => 'Menu Name',
'sub_menu' => true
) );