0

I built an Exercise custom post type with a custom field group using Advanced Custom Fields. The structure uses repeater fields for Week, Day, and Exercise, which creates a series of nested arrays. It looks like this:

Week 1
-- Day 1
---- Exercise 1
---- Exercise 2
-- Day 2
---- Exercise 1
---- Exercise 2
-- Day 3
---- Exercise 1
---- Exercise 2
Week 2
-- Day 1
---- Exercise 1
---- Exercise 2
-- Day 2
---- Exercise 1
---- Exercise 2
-- Day 3
---- Exercise 1
---- Exercise 2

I am able to display all of the contents using nested while loops, but what I want to do is display a singe Day's exercises.

The final page will look like this:

Week 2
Day 3
List of exercises

I have tried different ways to limit output, including using a counter, but that only allows me to stop after X rows. I tried using array_slice, which worked fine on the outer most array level for Weeks, but did not work on the inner arrays.

Any idea how I can display a specific day?

  • My question may be a bit messy. Let me simplify: I have a multidimensional array that is 5 layers deep; post, week, day, exercises, sets. I want to be able to display the contents of the array week 1, day 1, and all exercises and sets in the day 1 row. – David Cook Jan 20 '17 at 15:16
  • What does the array look like if you var_dump( $your_array )? – Milo Jan 20 '17 at 20:54
  • I actually figured out a way to make it work, though not very clean. Then I figured out the right way; using "while" loop an "if" statement and a counter I was able to get it to work. I will post the code below. – David Cook Jan 22 '17 at 2:32
0

I figured it out using a While loop and an If statement with a counter. This solution uses functions from Advanced Custom Fields;

$i = 0;  
while have_rows('week_rows') ): the_row();  
  $i++;  

  if( $i == 5 ) {  // stops on the 5th row of 'week_rows' array
    break;  
  }  

endwhile; // I had to move the endwhile up before my display statements 

// add display statements here to display array contents from 5th array row

?>  
  • This also works on deeper layers of a multi-dimensional array. In my case I used it to set the "Week" row, and then in the next dimension, to set the "Day" row. – David Cook Jan 26 '17 at 15:11

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.