0
votes

I'm working with ACF in WordPress.

I have a custom post type called Projects. Within there the user has the option to upload 2 featured images through an ACF repeater field.

Now, on the home page I've given the user the option to select 8 Post Object's from the Projects post type.

I need to be able to loop through this home page repeater field, and pull out both featured images and the project title from each 'project' post object.

ACF has recently depreciated the repeater_field function which I think it throwing me off here.

But, here's what I've been trying to work with so far:

<!-- check for repeater field -->
<?php if(get_field('featured-projects')): ?>

    <?php while(has_sub_field('featured-projects')): ?>

        <!-- get project post objects -->
        <?php $projects = get_sub_field('project'); ?>

        <!-- without the loop below, this echo's all 8 projects ID's -->
        <?php echo($projects->ID); ?><br />

        <!-- when added, only pulls the first project. And limits the echo above to the first ID -->
        <?php $loop = new WP_Query( array( 
            'post_type' => 'projects',
            'p' => $projects->ID
        ) ); ?>

        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>


        <?php endwhile; ?>


    <?php endwhile; ?>

<?php endif; ?>

I've tried to comment the code, but if anything does't make sense, let me know.

1

1 Answers

0
votes

Here is how i'd do it, though I realize its a departure from your method. I've included explanation in the code comments:

<?php $featured_projects = get_field('featured-projects'); //Set $featured_projects to equal the array of projects from the home page repeater. ?>

<!-- check for repeater field -->
<?php if($featured_projects): ?>

      <?php foreach($featured_projects as $featured_project) : //Loop through each featured project ?>

          <?php  $project_id = $featured_project['project']->ID; //Get the id for the current featured project ?>

          <?php $project_title = get_the_title($project_id); //set $title to be the title of the project ?> 
          <?php project_featured_images = get_field('name-of-featured-repeater-field-here', $project_id); //get the repeater field of the featured images from the project post ?>



          <h1 class='title'><?php echo $project_title; //print the title ?></h1>

          <?php if($project_featured_images[0]): //check if you have a 1st image (size large) ?> 
              <img class='featured-image-one' src="<?php echo $project_featured_images[0]['name-of-the-featured-image-sub-field-here']['sizes']['large']; //print the url to the 1st image; ?>"/>
          <?php endif; ?>

          <?php if($project_featured_images[1]): //check if you have a 2nd image ?> 
              <img class='featured-image-two' src="<?php echo $project_featured_images[1]['name-of-the-featured-image-sub-field-here']['sizes']['large']; //print the url to the 2nd image (size large); ?>"/>
          <?php endif; ?>

      <?php endforeach; ?>

<?php endif; ?>

Make sure to fill in the name of your project featured image repeater field, and the name of the image subfield within that repeater. This is clearly a more standard PHP based solution than the API version. I typically use this method per Elliot Candon's (ACF Developer) recommendation.

You can also get different image sizes of the featured images by changing the 'large' to another standard size, or by adding a custom size.