Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Richard Feinburg
Richard Feinburg
2,970 Points

Advanced Custom Fields and Repeater

I'm making a contact page with a list people's photo, name, title and short description. Everything is setup right for a repeater for Advanced Customs Field. The only problem I have is that I don't know how to setup the Department title that will be shown once and then the repeater for the people list will go after. Will like to do that as many times I want to as in if there is 5 different Department titles then I can add all 5 department title and the people list under each of them. I have the example of the code and a image of how I want it to look like if I didn't text it right.

alt text

<div class="col"><!-- Department Title Name -->
    <div class="grid_12">
        <h1><?php echo get_field('business_department'); ?></h1>
    </div>
</div>
<div class="col">
<?php if(get_field('contact_people')): ?>
<?php while(has_sub_field('contact_people')): ?>
<!-- Person Wrap -->
    <div class="grid_2 meet-wrap">
        <div class="meet-face"><!-- Face Photo -->
            <img src="<?php the_sub_field('contact_photo'); ?>" alt="" />
        </div>
        <div class="meet-text">
            <h2><?php the_sub_field('contact_name'); ?></h2><!-- Name -->
            <h3><?php the_sub_field('contact_title'); ?></h3><!-- Title -->
            <?php the_sub_field('contact_additional'); ?><!-- Short Description -->
        </div>
    </div>
<?php endwhile; ?>
<?php endif; // if( get_field('contact_people') ): ?> 
<!-- Person Wrap -->
</div><!-- Department Title Name -->

2 Answers

Clay Garland
Clay Garland
10,675 Points

You're going to have to nest two loops, and I'd probably go with a multidimensional array.

$departments = array('dept_name' => array('employee' => 'fred');

foreach($departments as $department) {
    foreach($department['dept_name'] as $employee) {
        echo $employee['employee'];
    }
}
Richard Feinburg
Richard Feinburg
2,970 Points

How do I apply the array? Not too get at this part of the ACF.

Clay Garland
Clay Garland
10,675 Points

I'm not sure what you're asking. If you're retrieving information from a database then you'll loop through the result set, building an array, and inserting employees into that array by department.

$departments = array();
while(mysql_fetch_object($result) = $employee) {
    $departments[$employee->department] = $employee;
}
Richard Feinburg
Richard Feinburg
2,970 Points

Hey, there is a better understand of what I'm trying to do. I did a work around. My ideal thing is to be about to add more Department with People cards without going into the code to add a new one.

http://dev.rsindustrial.com/meet-the-team/

Clay Garland
Clay Garland
10,675 Points

What you want to do is build an array of departments, and then each department will have and array of employees. That is if you are not getting information from a database.

$departments = array(
    array(
        'dept_name' => 'HR',
        'employees' = array(
            array(
                'name' => 'Fred',
                'title' => 'Admin Assistant'
                'description' => 'He\'s been here a long time.',
                'photo'=>'/images/fredpic.jpg'
            ),
             array(
                'name' => 'Jim',
                'title' => 'Admin Assistant'
                'description' => 'He\'s been here a long time.',
                'photo'=>'/images/pete.jpg'
            )
            array(
                'name' => 'Pete',
                'title' => 'Admin Assistant'
                'description' => 'He\'s been here a long time.',
                'photo'=>'/images/jim.jpg'
            )
        )
    ),
    array(
        'dept_name' => 'Management',
        'employees' = array(
            array(
                'name' => 'Joe',
                'title' => 'Manager'
                'description' => 'He\'s been here a long time.',
                'photo'=>'/images/joe.jpg'
            ),
             array(
                'name' => 'Jim',
                'title' => 'Admin Assistant'
                'description' => 'He\'s been here a long time.',
                'photo'=>'/images/pete.jpg'
            )
            array(
                'name' => 'Pete',
                'title' => 'Admin Assistant'
                'description' => 'He\'s been here a long time.',
                'photo'=>'/images/jim.jpg'
            )
        )
    ),
);

This is the kind of thing that needs to be database driven, but building arrays like that and iterating through the code would allow you to edit a single file by just adding names and never have to write HTML twice.

After you've got your array. . .

<?php foreach($departments as $department) { ?>
    <h1><?php echo $department['dept_name']; ?></h1><br>
    <?php foreach($department['employees'] as $employee) { ?>
        <div><?php echo $employee['name'] . $employee['title'] . $employee['description'] . $employee['pic']; ?></div>
    <?php }
 } ?>
Richard Feinburg
Richard Feinburg
2,970 Points

Will I be about to add more people and/or department in WordPress using ACF?

Clay Garland
Clay Garland
10,675 Points

I'm not sure how wordpress works exactly, but I know that it's database driven. There should be the capacity to add or remove records in wordpress, I have just never used it.

Richard Feinburg
Richard Feinburg
2,970 Points

Yea it will need to be able to hit a database WP.