Loop Parent Custom Posts with nested Childs
-
Hi All,
I’m attempting to put together a Food Menu for a Restaurant site using a hierarchical CPT I’ve created called ‘menuitem’ .
I’ve created a taxonomy called ‘menu’ and I’ve created taxononmy-menu.php in which I’d simply like to list all the menuitems for that menu (eg ‘Breakfast’, ‘Lunch’, ‘Dinner’ etc.)
I’ve managed to list the menuitems however I’d liked to list them with their children in nested lists (menuitms with parents are used for menuitem extras – EG Parent is ‘Eggs on Toast’ and child is ‘Extra Egg’)
EG- Toast
- Museli
- Eggs on Toast (Parent)
- Extra Egg (Child)
- Tomato (Child)
- Porridge
I’ve had a crack below but I don’t really know what I’m doing.
I can loop just parents or just children or both but not parents with nested children as described above.
Any help would be greatly appreciated.
Thanks in advance.
Mat
<ul class="deco-menu"> <?php global $post; $item_args = array( 'numberposts' => 50, 'offset'=> 1, 'post_type' => menuitem,'orderby' => 'title','order'=> 'ASC'); $items = get_posts( $item_args ); $parent_args = array( 'numberposts' => 50, 'offset'=> 1, 'post_type' => menuitem,'orderby' => 'title','order'=> 'ASC',post_parent => 0); $parents = get_posts( $parent_args ); $child_args = array( 'numberposts' => 50, 'offset'=> 1, 'post_type' => menuitem,'orderby' => 'title','order'=> 'ASC', post_parent => null); $children = get_posts( $child_args ); foreach( $items as $post ) : setup_postdata($post); $description = get_post_meta(get_the_ID(), 'ecpt_description', true); $price = get_post_meta(get_the_ID(), 'ecpt_price', true); ?> <?php if($parents){?> <li class="menu-item"><span class="item-title"> <?php echo the_title(); ?></span><span class="item-description"> <?php echo $description; ?></span></span><span class="item-price"> <?php echo $price; ?></span> </li> <?php;} else {?> <li class="menu-item"><span class="item-title"> <?php echo the_title(); ?></span><span class="item-description"> <?php echo $description; ?></span></span><span class="item-price"> <?php echo $price; ?></span></li> <ul class="sub-menu"> <li class="menu-item-child"><span class="item-title"> <?php echo the_title(); ?></span><span class="item-description"> <?php echo $description; ?></span></span><span class="item-price"> <?php echo $price; ?></span></li> </ul> <?php;}?> <?php endforeach; ?> </ul>
- The topic ‘Loop Parent Custom Posts with nested Childs’ is closed to new replies.