Home › Forums › Add-ons › Repeater Field › Post Object in Repeater Field. Need Rows Count
I have a Repeater field (field name is “positions”) with a Post Object field (field name is “position”) as the field that repeats. I’d like to be able to count how many “position” the user has added and customize the front-end so that everything displays correctly.
I found the following code at https://support.advancedcustomfields.com/forums/topic/if-repeater-field-has-1-rows-2-rows-etc/. It helped me, but $count is returning 1 when it should be 2.
<?php $count = count( get_sub_field( 'position' ) );
if ( $count == 1 ) { ?>
<div class="full-width">
<?php } elseif ( $count == 2 ) { ?>
<div class="half-width">
<?php } elseif ( $count >= 3 ) { ?>
<div class="one-third">
<?php } ?>
Like I said, the code above is working, but $count is returning 1, not 2.
Hi @phej
I believe you need to count the repeater field instead of the subfield like this:
$count = count( get_field( 'positions' ) );
I hope this helps 🙂
Hey @acf-support,
I tried $count = count( get_field( 'positions' ) );
but <?php echo $count;?>
still echos “1”
Here’s the field in the admin: and here it is on the page:
. Not sure if that helps or not.
Hi @phej
That’s weird. Could you var_dump() the returned data like this:
echo <pre>;
var_dump(get_field( 'positions' ));
echo </pre>;
Also, could you please try to add the post/page ID as the second parameter of get_field() like this:
echo <pre>;
var_dump(get_field( 'positions', 99 ));
echo </pre>;
Where ’99’ is the post/page ID that has the repeater field.
Thanks 🙂
get_field( ‘positions’ ) yielded:
array(2) {
[0]=>
array(1) {
["position"]=>
object(WP_Post)#597 (24) {
["ID"]=>
int(243)
["post_author"]=>
string(1) "1"
["post_date"]=>
string(19) "2016-06-17 14:35:53"
["post_date_gmt"]=>
string(19) "2016-06-17 14:35:53"
["post_content"]=>
string(1271) "
get_field( ‘positions’, 10 ) yielded:
array(2) {
[0]=>
array(1) {
["position"]=>
object(WP_Post)#597 (24) {
["ID"]=>
int(243)
["post_author"]=>
string(1) "1"
["post_date"]=>
string(19) "2016-06-17 14:35:53"
["post_date_gmt"]=>
string(19) "2016-06-17 14:35:53"
["post_content"]=>
string(1271) "
As well as the content for both posts.
Hi @phej
That’s weird. As you can see, the var_dump() returns array(2), which mean the count() function should return “2” too. Could you please try it like this:
echo count(get_field( 'positions' ));
And:
echo count(get_field( 'positions', 10 ));
If it still returns ‘1’, could you please open a new ticket and provide temporary credentials to your site here: https://support.advancedcustomfields.com/new-ticket?
Don’t forget to include the URL to this thread and let know where you can find the code in your template file.
Thanks 🙂
Yeah, it is weird.
I made a comment with the URL, but it’s private – not sure if you can see that or not – as the site is still in dev. I’ll try that tomorrow and open a ticket if it doesn’t work.
Thanks!
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!
CPT registration is coming to ACF! We demoed the new feature during the most recent session of ACF Chat Fridays. Check out the summary for the details. https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 7, 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.