Home › Forums › Add-ons › Repeater Field › How do I get the first row of a repeater, and get a sub_field from it?
I read the documentation but it doesn’t work.
I searched in other threads, and what I found doesn’t work. I have this massive piece of code:
<?php
$current_category = get_queried_object_id();
$taxonomy = 'brands';
// Get all products from this category
$tax_query = array(
array(
'taxonomy' => 'brands',
'field' => 'term_id',
'terms' => $current_category,
'operator' => 'IN',
)
);
$args = array(
'post_type' => 'products',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'asc',
'tax_query' => $tax_query
);
$products = get_posts( $args );
// Create your new array to store your products in, organized by brand
$products_sorted = [];
if ($products):
// Loop through all products belonging to this category
foreach($products as $product):
$product_id = $product->ID;
$product_brands = get_the_terms($product_id, $taxonomy); // Return an array.
// For each returned brand (cause it's an array), add the product to the final sorted array.
// We're going to only add the product ID for simplicity, all other product values can be grabbed from the ID later
foreach ($product_brands as $brand):
$products_sorted[$brand->term_id][] = $product_id;
endforeach; // $product_brands loop
endforeach; // $products loop
// Now that all our products are in an array organized by brand, let's sort the brands by alphabetical order
// Check the result to make sure everything is organized correctly, if the array isn't showing properly then the output won't work
// Show all products under their brand groups, in an unordered list for simplicity
foreach($products_sorted as $brand_id => $brand_products):
$brand = get_term( $brand_id, $taxonomy);
$gap = '<hr class="x-gap" style="margin: 80px 0 0 0;">';
echo '<h2>' . $brand->name . '</h2>';
echo $gap;
echo '<div class="tax-products">';
foreach ($brand_products as $product_id):
$product_featured = get_the_post_thumbnail_url( $product_id, 'full' );
$product_default = get_field('default_product_image', 'option');
$flavour_row = get_field('flavours' ); // get all the rows
$product_title = get_the_title($product_id);
$product_link = get_the_permalink($product_id);
echo '<div class="tax-product-item x-column x-sm x-1-3">';
if( $product_featured ) :
echo '<a href="' . $product_link . '"><img class="tax-product-img" src="' . $product_featured . '" alt="' . $product_title . '" /></a>';
elseif ( $flavour_row ) :
if (have_rows('flavours')) :
while (have_rows('flavours')) :
the_row();
// code here to show image
$first_flavour = get_sub_field('flavour_image');
echo '<a href="' . $product_link . '"><img class="tax-product-img" src="' . $first_flavour . '" alt="' . $product_title . '" /></a>';
// exit loop after first image
break;
endwhile;
endif;
else :
echo '<a class="tax-product-default" href="' . $product_link . '"><img class="tax-product-img" src="' . $product_default . '" alt="' . $product_title . '" /></a>';
endif;
echo '<div class="tax-product-meta">';
echo '<h6><a href="' . $product_link . '">' . $product_title . '</a></h6>';
echo '</div>'; // .tax-product-meta
echo '</div>'; // .tax-product-item
endforeach; // $brand_products product_id loop
echo '</div>'; // .tax-products
endforeach; // $products_sorted brand loop
endif; // $products check
?>
This is the page I’m working on.
I have no idea why it’s grabbing the image that it is, if you click on each product you’ll see the image I’m trying to grab for each, the first one in the carousel.
The main part in the code is the elseif
part.
You must be logged in to reply to this topic.
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!
🤔 Curious about the ACF user experience? So are we! Help guide the evolution of ACF by taking part in our first ever Annual Survey and guarantee you’re represented in the results. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 8, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.