WordPress.org

Ready to get started?Download WordPress

Forums

Loop Parent Custom Posts with nested Childs (5 posts)

  1. eclessia
    Member
    Posted 2 years ago #

    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>
  2. bcworkz
    Member
    Posted 2 years ago #

    You have the right idea, initiate a <ul> sub list for sub menu items. But your code logic is flawed, you're using the results from a query outside of your loop to dictate potential changes inside the loop. Since the query results don't change, it will not work.

    There's a number of approaches, but assuming you only have two levels, a relatively crude approach may be the easiest to understand and implement. You can refine it later if you wish.

    1. Before the main loop, query for main menu items and output the initial <ul> tag.
    2. Initiate the main loop of main menu items. For each main menu item, output the item wrapped in appropriate tags.
    3. Also for each main menu item, query for children of that one menu item inside the main loop. If no results, do nothing except go on to 4., otherwise go to 3a.
    3a. Output the initial <ul> tag. Loop through children and output each child wrapped in appropriate tags. After the child loop is exhausted, output the closing </ul> tag.
    4. Continue to next main menu item until exhausted. Output the closing </ul> tag.

    This is very inefficient, but it doesn't matter for small databases. It's possible to query everything at once, and create a re-entrant function that will output a nested list any number of levels deep just based on the ID values in ID and post_parent fields. Such functions can be tricky to implement so that they don't get stuck in infinite loops. Sometimes simple brute force straight forward solutions work just fine.

  3. eclessia
    Member
    Posted 2 years ago #

    Thanks bcworkz,

    I follow what you mean but I'm not quite sure how to implement it.

    I had a crack at something like this - with no success.

    Would you mind putting me on the right track?

    Thanks again!

    <?php
    							global $post;
    							$item_args = array( 'numberposts' => 50, 'offset'=> 1, 'post_type' => menuitem,'orderby' => 'title','order'=> 'ASC');
    							$items = get_posts( $item_args );
    							echo '<ul class="deco-menu">';
    							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
    							$parents = array('post_parent' => 0);
    							if($parents['post_parent'] == 0){?>
    
    								<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>	
    
    										<?php;} else {?>
    										nothing yet
    
    										<?php;}?>		
    
    						<?php endforeach; ?>
  4. eclessia
    Member
    Posted 2 years ago #

    Does anyone else want to weigh in?

    I've not made much progress with it.

    Cheers

    Mat

  5. eclessia
    Member
    Posted 2 years ago #

    I came up with a work around - I just added a new metabox feild (extas) but it's not quite the way I'd like to do it.

    <?php if (have_posts()) : ?>
    		<?php while (have_posts()) : the_post(); ?>
    
    		<article class="item" data-permalink="<?php the_permalink(); ?>">
    
    						<?php
    
    							$description = get_post_meta(get_the_ID(), 'ecpt_description', true);
    							$price = get_post_meta(get_the_ID(), 'ecpt_price', true);
    							$extras = get_post_meta(get_the_ID(), 'ecpt_extras', true); ?>
    
    										<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><div class="item-extras"> <?php  echo $extras; ?></div>
    										</li>
    		</article>
    
    		<?php endwhile; ?>
    	<?php endif; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic