0

I am currently trying to create a drop down feature with the repeater custom field input. This may or may not be the right custom field to be used but it seems like it is given the other offered options. So, this is what my custom field looks like on the admin panel:

Repeater Custom Field

It is very simple what I am trying to do. I am trying to display the first three options of this list. I then want to add a SEE MORE button that the user can click and it will show all subsequent updates. Example:

enter image description here

I am using bootstrap for handling the accordion feature. The issue that I am having is splitting the data appropriately. Getting the first three rows into the container displaying on the page... and the rest into the bootstrap accordion container. My thought was to approach this by creating two arrays. Slicing the first three rows into the first array and then slicing from row four on into the second array. The only problem I am having is handling the data that comes in from the backend. It is very confusing and I may be going about this in a wrong way.

The first thing I noticed was that each row from the admin panel comes in as separate arrays when using the get_row() function. NOT separate arrays wrapped in array: How it comes in: [] [] [] [] How it does NOT come in: [[], [], [], []] This obviously means I cannot perform php array functionality on these as a group because they are not in an array. So I decided to put them in an array array_push($new_array, $rows). This KINDA works as I receive an output of:

Array ( 
[0] => Array (
    [field_598a36a3cea71] => July 5, 2017
    [field_598a36bdcea72] => ONE
    )
[1] => July 5, 2017
[2] => ONE
[3] => Array (
    [field_598a36a3cea71] => July 6, 2017
    [field_598a36bdcea72] => TWO
    )
[4] => July 6, 2017
[5] => TWO
[6] => Array (
    [field_598a36a3cea71] => July 7, 2017
    [field_598a36bdcea72] => THREE
    )
[7] => July 7, 2017
[8] => THREE
[9] => Array (
    [field_598a36a3cea71] => July 8, 2017
    [field_598a36bdcea72] => FOUR
    )
[10] => July 8, 2017
[11] => FOUR )

As you will see, every third Array() is a complete array, but it is also returning every single value that exists... So if I do print_r(count($new_array)) this is going to come back as 12 arrays in my $new_array. Four being each row coupled, the next eight being every single item within my rows. Here is the code that I am playing with right now:

<?php if(have_rows('updates_list')): ?>
  <?php $new_array = array() ?>
    <?php while(have_rows('updates_list')): the_row(); ?>
      <?php $rows = get_row(); ?>
      <?php array_push($new_array, $rows) ?>
    <?php endwhile; ?>
  <?php print_r($new_array) ?>
<?php endif; ?>

Quite frankly, because I am so new I do not know if I am on the right path, using the right custom field, writing syntactically correct my php... So any ideas, help, suggestions on how I can achieve this I would greatly appreciate it. I will tentatively be watching any questions and comments that are posted, so expect a relatively quick response from myself. Thank you in advance to anyone who helps or assists in any way.

  • 1
    Try: $fields = get_field("updates_list"). And then print_r to see what's inside. You should have a nice looking array with all repeater fields and values inside. Then stop using have_rows and get_row and just loop using a foreach loop. – gdaniel Aug 9 '17 at 19:38
  • @gdaniel If you have a second to take your comment and submit it as an answer I will go ahead and mark it as correct. The point in case was that I was simply using the wrong function to get the custom fields. get_row() coupled with the_row() was creating the issue. I needed to return an array of each row as an array. get_field() is the function for this. – Nappstir Aug 9 '17 at 20:17
1

Try: $fields = get_field("updates_list"). And then print_r to see what's inside. You should have a nice looking array with all repeater fields and values inside. Then, stop using have_rows and get_row and just loop using a foreach loop.

get_field() is usually used to retrieve a unique field, but because it's a repeater field you get all the sub_fields with it. As long as you know how to get values from the array, you don't need the acf loop functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.