Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

ACF repeater field on options page + repeater field on page WordPress

  • SOLVED

Essentially what I have is an options page with a repeater field and in that a column that has multiple checkboxes. Each row has a number of these checkboxes (in the code below, the staff_teams field) ticked. Using the code below I can get it happily working so that if a checkbox has a value of ‘The Team’, and that has been checked, then that displays on the page. This works great however on my page template I have a repeater which, for each row, has a text field where a value is entered. I need this value to be dynamically used in the code below instead of a static value (where ‘The Team’ currently is) as this will correspond with the checkboxes (and will change per row). I’ve been playing with calling that page repeater inside this (if/while rows) but it's not working.

The code that works ::


<?php
if(get_field('staff_profiles', 'option')):
while(has_sub_field('staff_profiles', 'option')):
$team = get_sub_field('staff_teams’);

if(
(get_field('staff_profiles', 'option')) &&
(is_array($team) && in_array( ’The Team', $team ))
):
?>

<div>
a heap of things to show everything i want for the profile here using the options page rows
</div>

<?php else: ?>

<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>


For clarity calling a repeater field from the page template looks like this ::


<?php if( have_rows('repeater') ): ?>
<?php while( have_rows('repeater') ): the_row();
$code = get_sub_field('the_code');
?>
<div>
<?php echo $code; ?>
</div>
<?php endwhile; ?>
<?php endif; ?>


So essentially getting that $code into the 'The Team' section above :)

Answers (3)

2015-03-21

Arnav Joy answers:

are you able to get into this if statement ?

if(get_field('staff_profiles', 'option')):


elle007 comments:

That if statement works fine for pulling the data from the ACF 'Options Page'. I need to grab the text field from inside a repeater on the actual page template (just a standard ACF repeater field) within this. I.e whatever that text field in the repeater on the page says, that value is used instead of 'The Team' in the above code.

Does that make sense?


Arnav Joy comments:

can you let me know what this produces :-

<?php if( have_rows('staff_profiles') ): ?>

<?php while( have_rows('staff_profiles') ): the_row();

$team = get_sub_field('staff_teams');

?>

<div>

<?php print_r($team ); ?>

</div>

<?php endwhile; ?>

<?php endif; ?>


elle007 comments:

Nothing as the staff_profiles is from an ACF options page so the code above doesn't work like that to pull those fields but if I do that with the correct options code so the following ::


<?php if(get_field('staff_profiles', 'option')): ?>
<?php while(has_sub_field('staff_profiles', 'option')):
$team = get_sub_field('staff_teams');
?>

<div>
<?php print_r($team ); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>



I get the array values from the checkboxes naturally :)


Array ( [0] => TeamOne [1] => TeamTwo [2] => TeamThree [3] )
Array ( [0] => TeamOne [1] => JuniorTeamOne )
Array ( [0] => TeamOne [1] => TeamTwo [2] => JuniorTeamTwo )

2015-03-22

Romel Apuya answers:

what is the name of your repeater field??

<?php
// check if the repeater field has rows of data
if( have_rows('repeater_field_name') ):
// loop through the rows of data
while ( have_rows('repeater_field_name') ) : the_row();
// display a sub field value
the_sub_field('sub_field_name');
endwhile;
else :
// no rows found
endif;
?>

should work..replacing repeater_field_name and sub_field_name.

also what is the settings of the repeater field location rule???


Romel Apuya comments:

a simple debug too try


$rows = get_field('repeater_field_name');

where repeater_field_name it the name of your repeater.

then do

var_dump($rows);


then loop if it has contents.

if($rows)
{
echo '<ul>';

foreach($rows as $row)
{
echo '<li>sub_field_1 = ' . $row['sub_field_1'] . ', sub_field_2 = ' . $row['sub_field_2'] .', etc</li>';
}

echo '</ul>';
}




elle007 comments:

Where are you suggesting I put that standard code to loop through the repeater for it to replace the 'The Team' in the array?


Romel Apuya comments:

in the page template that you have.


Romel Apuya comments:

actually the step is a debugging step.
can you provide a screenshot of th repeater field settings and the page that is using your page template?
Ive sent you a message so we can communicate faster.


Romel Apuya comments:

replace this

<?php if( have_rows('repeater') ): ?>
<?php while( have_rows('repeater') ): the_row();
$code = get_sub_field('the_code');
?>
<div>
<?php echo $code; ?>
</div>
<?php endwhile; ?>
<?php endif; ?>


width


<?php
$rows = get_field('repeater_field_name');
if($rows)
{ ?>
<ul>
<?php foreach($rows as $row){ ?>
echo $row['the_code'];
<?php } ?>
</ul>
<?php } ?>


Romel Apuya comments:

replace also the repeater_field_name from above with the field name of ur repeater.


Romel Apuya comments:

Hi,
Here's the code that i have generated. For the options page.

<?php
/*get id of pages thats using
change meta_value relative to path of the file ie. page-template/page-junior.php
*/
$pages = get_posts(array(
'post_type' => 'page',
'meta_key' => '_wp_page_template',
'meta_value' => 'page-junior.php'
));
/*Store page ID to array*/
$pageids = array();
foreach($pages as $page){
$pageids[] = $page->ID;
}

/*Get all squad texts*/
$squad_text =array();

foreach($pageids as $pageid){
if(get_field('junior_squad')):
while(has_sub_field('junior_squad')):
$squad_text[]= get_sub_field('squad_code', $pageid);
endwhile;
endif;
}

if(get_field('staff_profiles', 'option')):
while(has_sub_field('staff_profiles', 'option')):
$team = get_sub_field('staff_teams');
foreach($squad_text as $squad_texts){
if((get_field('staff_profiles', 'option')) &&(is_array($team) && in_array( $squad_texts, $team ))):
?>
<div>
a heap of things to show everything i want for the profile here using the options page rows
</div>
<?php else: ?>
<?php endif; ?>
<?php } ?>
<?php endwhile; ?>
<?php endif; ?>

2015-03-23

Andrea P answers:

I'm sorry it's very late here and I'm going to bed, I'll try to better explain this tomorrow.

anyway, I think you should use a foreach instead of a while loop when you grab the fields from one of the repeaters (probably would be better if you use the foreach for the options repeater, and the normal while loop for the template one). probably having a nested while loop is messing up with the function for getting the sub_fields.

have a look at this page (scroll down till you see the foreach PHP loop):
http://www.advancedcustomfields.com/resources/repeater/

UPDATED:

if I understood correctly your needs, this could be the code for your page template:


<?php if( have_rows('repeater') ): ?>

<?php while( have_rows('repeater') ): the_row();

$code = get_sub_field('the_code');

?>

<div>

<?php
$staff_profile_rows = get_field('staff_profiles', 'option');
if($staff_profile_rows):

foreach ($staff_profile_rows as $staff_profile):

// you can now access all subfields as you'd do for an array, placing the field slug as the array key:
// e.g. your staff_teams field for this row will be stored here:
$team = $staff_profile['staff_teams'];


if( is_array($team) && in_array( $code, $team) ): // you don't need to check if staff_profile is defined, as it will surely be if it's into a foreach loop

?>

<div>

a heap of things to show everything i want for the profile here using the options page rows
<!-- remember that the variable $team is an array, so to access your checkboxes you'll have to make a loop, or access them by their index
like: $team[0] if you want to get the first checkbox -->

</div>

<?php else: ?>


<?php endif; ?>

<?php endforeach; ?>

<?php endif; ?>

</div>

<?php endwhile; ?>

<?php endif; ?>