WordPress.org

Ready to get started?Download WordPress

Forums

Error with Custom Post & Meta Nested Loops (1 post)

  1. zenhaus
    Member
    Posted 3 years ago #

    Hello,

    I am attempting display custom post data and meta data on a Home page. There are two different custom post categories, and the meta data is part of the Home page.

    My code is below, and a brief outline of the nested loop structure is as follows:

    Begin main page content loop
    Pull in some meta data for current page
    ---
    Begin first custom post type loop
    Pull in first custom post data
    End first custom post type loop
    ---
    Pull in more meta data for current page
    ---
    Begin second custom post type loop
    Pull in second custom post data
    End second custom post type loop
    ---
    End main loop

    My second set of meta data (quote and testimonial_author) is not showing up. However, if I remove the first custom post type loop it shows up just fine. I'm guessing there is something wrong with my loop.

    Here's my code.

    <?php
    /*
    Template Name: Home
    */
    ?>
    
    <? get_header(); ?>
    
    <div class="left">
    
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    
    	<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    		<!--
    		<?php if ( is_front_page() ) { ?>
    			<h2 class="entry-title"><?php the_title(); ?></h2>
    		<?php } else { ?>
    			<h1 class="entry-title"><?php the_title(); ?></h1>
    		<?php } ?>
    -->
    		<div class="entry-content">
    
    			<?php the_content(); ?>
    
    			<!-- get meta values -->
    			<?php
    
    			  $mykey_values = get_post_custom_values('additional_text');
    			  foreach ( $mykey_values as $key => $value ) {
    			    echo "<h2>$value</h2>";
    			  }
    
    			?>  
    
    			<div style="margin: 20px 0 0 0;">
    
    				<!-- nested loop to pull in custom post data for home-features -->
    				<?php 
    
    				$my_posts = new WP_Query();
    				$my_posts->query('post_type=home-features'); // what to get
    					while($my_posts->have_posts()) : $my_posts->the_post(); // loop for posts
    
    				  ?>
    				  <h3><?php the_title(); ?></h3>
    				  <?php
    				  $subhead_val = get_post_custom_values('subheading');
    				  foreach ( $subhead_val as $key => $sub_value ) {
    				    echo "<p><em>$sub_value</em></p>";
    				  }
    
    				  $desc_val = get_post_custom_values('description');
    				  foreach ( $desc_val as $key => $desc_value ) {
    				    echo "<p>$desc_value</p>";
    				  }
    
    				 endwhile;
    				 ?>
    				 <!-- end nested loop for home-features custom post data -->
    			</div>
    
    			<p>See more of <a href="/upcoming-bookings/">Carol's presentations ยป</a></p>					
    
    		</div><!-- .entry-content -->
    	</div><!-- #post-## -->
    
    </div> <!-- /.left -->
    <div id="primary" class="widget-area" role="complementary">
    
    	<?php get_search_form(); ?>
    
    	<!-- pull in additional Home page meta data in sidebar -->
    
    	<div id="testimonial">
    	<?php
    
    	$mykey_values = get_post_custom_values('quote');
    	foreach ( $mykey_values as $key => $value ) {
    	echo "<p>$value</p>";
    	}
    
    	?>
    
    	<?php
    
    	$mykey_values = get_post_custom_values('testimonial_author');
    	foreach ( $mykey_values as $key => $value ) {
    	echo "<p class='name'>$value</p>";
    	}
    
    	?>
    	</div> <!-- /#testimonial -->
    
    	<!-- get category list -->
    	<div id="category-list">
    	<h3>Choose Your Category</h3>
    
    	<!-- nested loop to pull in custom post data for speaking categories -->
    	<?php $recent = new WP_Query('post_type=speaking-categories'); while($recent->have_posts()) : $recent->the_post(); ?>
    
    <ul>
    <li><a>"><?php the_title(); ?></a></li>
    </ul>
    	<?php endwhile; ?>
    	<!-- end nested loop for speaking categories custom post data -->
    
    	</div> <!-- /#category-list -->
    
    	<?php endwhile; ?>
    	<!-- end main loop -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    Does anyone have any ideas why my nested loop is preventing the additional meta data from appearing on the page?

Topic Closed

This topic has been closed to new replies.

About this Topic