Home › Forums › Add-ons › Repeater Field › What does the_row() return?
I have a repeater named resources
with 8 fields. The first field is a radio field with three options. Depending on your choice, a subset of the remaining 7 fields are shown. For context, these are resources for a non-profit web site and may include links to internal pages/posts/cpt (via relationship field), external links (text field) or internal documents (file field). On my template, I loop through the rows and need to switch the HTML output (and sub_field() calls) depending on what type of content it is, as indicated by the radio field value.
The radio field, called “resource-type” has these values
internal : Internal Link
external : External Resource
doc : Document
In my test content I have 4 rows:
This code, outputs what I would expect:
Code
<?php
if( have_rows('resources') ):
while ( have_rows('resources') ) : the_row();
$type = get_sub_field('resource_type');
echo "<h3>Type: " . $type . " </h3>";
endwhile;
endif;
?>
OUTPUT
Type: internal
Type: external
Type: doc
Type: internal
But, after reading “The the_row function also returns the row data as an array if you wish to store it as a variable and use it.” on http://www.advancedcustomfields.com/resources/functions/have_rows/#notes I thought that this would output the same thing:
<?php
if( have_rows('resources') ):
while ( have_rows('resources') ) : the_row();
$row = the_row();
$type = $row['resource_type'];
echo "<h3>Type: " . $type . " </h3>";
endwhile;
endif;
?>
But it’s only outputting:
Type: external
Type: internal
What am I missing?
Hi @Will
The issue is that you are using the_row
twice within the loop!
Only use it once per loop like so:
<?php
if( have_rows('resources') ):
while ( have_rows('resources') ) : $row = the_row();
$type = $row['resource_type'];
echo "<h3>Type: " . $type . " </h3>";
endwhile;
endif;
?>
Thanks
E
Ahhhhhhhhhhhh! THANK YOU!
Makes total sense – it was getting the next row each time so it only echoed the 2nd and 4th items.
Just in case someone stumbles across this in 2017 onwards: it turns out that contrary to what is stated in the docs for get_row() and the_row(), both functions return arrays keyed by field internal names and not field user defined names. So you will get:
Array
(
[field_5855ac26cb153] => 'John'
[field_5855ac26cb154] => 'Smith'
[field_5855ac26cb155] => 31
)
…instead of:
Array
(
[name] => 'John'
[surname] => 'Smith'
[age] => 31
)
…as the docs would have you believe. This makes both functions more or less useless for the end user. It has been raised with support back in September that either both functions should return arrays keyed by field names or the docs should be changed as they are misleading. Neither has been done.
As an update to the previous comment since 5.3.3 of ACF Pro you can pass a true
bool to the_row / get_row i.e. the_row(true)
This will output your keynames as expected and originally explained in previous comments
This is explained in the docs found here: https://www.advancedcustomfields.com/resources/get_row/
cc @davidcie
@fatsoma thanks for the update! This definitely fixes one of my ACF pet peeves.
The docs are still incorrect thou. The code example should say $row = get_row(TRUE);
rather than $row = get_row();
because $format_values
defaults to FALSE
.
The topic ‘What does the_row() return?’ 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.