Home › Forums › Front-end Issues › 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();
?>
The topic ‘sub_fields in a foreach loop’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.