Support

Account

Home Forums Front-end Issues sub_fields in a foreach loop

Solved

sub_fields in a foreach loop

  • I’m trying to reverse a repeater field I have that contains track listings. How can I get the subfield to display?

    if( have_rows('track_listing') ):
    
      $tracks_forward = get_field('track_listing');
      $tracks_reversed = array_reverse($tracks_forward);
    
      foreach ($tracks_reversed as $track):
        the_sub_field('track_artist');
      endforeach;
    
    endif;

    Thanks.

  • As per ”, I should have done the following code works…

    <?php
    
    // Define custom query parameters
    $custom_query_args = array (
    	'post_type'              => array( 'post' ),
    	'category_name'          => 'thebigshow',
    	'pagination'             => false,
    	'posts_per_page'         => '-1',
    	'order'                  => 'DESC',
    );
    
    // Instantiate custom query
    $custom_query = new WP_Query( $custom_query_args );
    
    // Output custom query loop
    if ( $custom_query->have_posts() ) :
    
    	$track_number = 0;
    	echo '<table class="sortable">';
    
    		echo '<tr>';
    		echo '<th>ID</th>';
    		echo '<th>Artist</th>';
    		echo '<th>Track</th>';
    		echo '<th>Date</th>';
            echo '<tr>';
    
        while ( $custom_query->have_posts() ) :
            $custom_query->the_post();
    
            if( have_rows('track_listing') ):
    
            	$tracks_forward = get_field('track_listing');
    			$tracks_reversed = array_reverse($tracks_forward);
    
    			 	// loop through the rows of data
    			    foreach ($tracks_reversed as $track):
    
    			    	$track_number = $track_number+1;
    
    			    	echo '<tr>';
    
    			        // display a sub field value
    			    	echo '<td class="track_number">';
    			        echo $track_number;
    			        echo '</td>';
    
    			        echo '<td>';
    			        echo $track['track_artist'];
    			        echo '</td>';
    
    					echo '<td>';
    			        echo $track['track_title'];
    			        echo '</td>';
    
    			        echo '<td>';
    			        echo get_post_time('jS M Y');
    			        echo '</td>';
    
    			        echo '</tr>';
    
    			    endforeach;
    
    			else :
    
    			    // no rows found
    
    			endif;
    
        endwhile;
    
        echo '</table>';
    
    endif;
    
    // Reset postdata
    wp_reset_postdata();
    
    ?>
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘sub_fields in a foreach loop’ is closed to new replies.