Support

Account

Home Forums Add-ons Repeater Field Accessing Post Object in a Nested Repeater

Solved

Accessing Post Object in a Nested Repeater

  • I have been reading the forums and StackOverflow all day long trying to figure this out.

    I have attached a screenshot of a mockup of the table (labeled), how it looks live, and what my custom post type looks like in the back-end.

    What I am trying to accomplish is to display the post objects featured image in the table head row. Then I’d like the “legend titles” to display in the first row of the table body and the attributes filling in the rest of the row.

    I am able to get the legend titles and attributes to display fine however I can’t get the post objects featured images to display in the table head. When I play with my code to display the featured image, it only shows 1 (when it should be 5) and the legend tiles and attributes disappear.

    Here is my code: https://gist.github.com/primedime/3d79cd5d01e794b659cb8e4f07603715

  • This has been solved. The code is below.

    
    <?php
    /**
     * The template for displaying all pages
     *
     * Template Name: Feature Comparison
     *
     * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
     *
     * @package Merchant_Services
     */
    
    get_header();
    ?>
    
    <div id="primary" class="content-area">
    	<main id="main" class="site-main">
    
        <?php get_template_part( 'template-parts/content','hero' ); ?>
    		
    		<div class="comparison-box-wrapping">
    			<?php $loop = new WP_Query( array( 
    				'post_type'      => 'comparison_tables',
    				'orderby'		     => 'date', 
    				'order'          => 'DESC', 
    				'posts_per_page' => '5') ); 
    				?>		
        	<?php while( $loop->have_posts() ) : $loop->the_post(); ?>
    
    				<div class="comparison-block">				
    					<div class="recommend-header"><h2 class="green-medium"><?php the_title(); ?></h2></div>				
    						<table>
    							<thead>
    								<tr>
    									<th>&nbsp;</th>	
    									<?php $legends = get_field('legend');
    									if($legends){
    										
    										foreach($legends as $legend){
    											$company_attributes = $legend['company_attributes'];
    											foreach ($company_attributes as $company_attribute) {
    												$companies = $company_attribute['company'];
    												$count++;
    												
    												//limit the count to only get the logos from the first legend
    												if( has_post_thumbnail($companies->ID) && $count < 6) { 
    													echo "<th>";
    														$thumb = get_the_post_thumbnail($companies->ID, 'full');
    														echo $thumb;
    														echo "</th>";     
    												};
    												
    											}
    										}
    									} ; ?>
    																										   										        		
    								</tr>
    							</thead> 
    							<tbody>	
    							<?php if ( have_rows( 'legend' ) ) : ?>												
    								<?php while( have_rows('legend') ): the_row();
    									$legend_title  = get_sub_field('legend_title');
    									?>
    									<tr>
    										<td><p><?php echo $legend_title; ?></p></td>								
    											<?php while ( have_rows( 'company_attributes' ) ) : the_row(); 
    												$attribute  = get_sub_field('attribute');										
    												?>
    												<td><?php echo $attribute; ?></td>
    											<?php endwhile; ?>     			
    									</tr> 
    								<?php endwhile; ?>
    								<?php endif; ?>					     						
    							</tbody>
    							
    						</table>					
    				</div> <!-- comparison-block END -->
    			<?php endwhile; ?>
    			
      	</div> <!-- comparison-box-wrapping END -->
        
    		<?php get_template_part( 'template-parts/content','disclosure-dark' ); ?>
    	</main><!-- #main -->
    </div><!-- #primary -->
    
    <?php
    get_footer();
    
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Accessing Post Object in a Nested Repeater’ is closed to new replies.