I’m trying to have a different layout if there are more than 1 rows in a repeater/flexible content.
So if there is only 1 item/row it shows x if there are more than 1 so 2 or more it shows y.
Tried the below but its adding the style to the first row regardless, rather than counting the rows and spitting out the different content.
<?php if ( have_rows( 'row_content_boxes' ) ):?>
<?php while ( have_rows( 'row_content_boxes' ) ) : the_row();
$is_single_post = get_row_index();
?>
<?php if ( $is_single_post >= 2 ): //If has 2 rows?>
If only has 2 or more item in row show this
<?php else: ?>
If only has 1 item in row show this
<?php endif; ?>
<?php endwhile; ?>
<?php else: ?>
<?php // no rows found ?>
<?php endif; ?>
Not exactly sure what you’re trying to do to be honest.
get_row_index() will only give you the current index, not the number of rows. This is good if you want to alter something based on the current row, but not if you are trying to change something based on the number of rows.
if you want to get the number of rows
$rows = count(get_field('row_content_boxes'));
I’ve a repeater/flexible content and it has 2 rows in it, i want it to show a certain div however, if its only got 1 i want it to show another.
Whatever i try it always adds it to the first row even if there is 2, when i only want it to add to the first one if thats the only one, not the first one of 2.
Does that explain better?
<?php
$row_count = count(get_field('row_content_boxes'));
if (have_rows('row_content_boxes')) {
if ($row_count == 1) {
// only one row
} else {
// more than one row
}
while (have_rows('row_content_boxes')) {
the_row();
// code for each row here
}
}
?>
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!
We’re hard at work on ACF 6.1, and Beta 1 is now available 🚀
— Advanced Custom Fields (@wp_acf) March 16, 2023
This release includes custom post type and taxonomy registration, an improved experience when selecting field types, PHP 8.1 and 8.2 compatibility, and more!
Let’s take a look 🧵https://t.co/Y0WcAT11l4
© 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.