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(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php endwhile; ?>
<?php endif; ?>
I've tried to comment the code, but if anything does't make sense, let me know.
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.
Related
I'm having trouble wrapping my head around this on a WordPress site using ACF pro. Essentially I have an ACF Post Object field that allows the user to manually select events posts that are related to the current event post and displays them on the page. The users will draft events once they have past instead of deleting them. Everything is working fine except in the instance that all the related events have been "drafted" than I don't know how to do a check to remove the parent section. So if all the selected posts are changed to draft I'm not just left with just <h2>Related Events</h2>.
This is my current code:
<?php
$featured_posts = get_field('post_summary_events');
if (!empty($featured_posts)) : ?>
<div id="related-events">
<h2>Related Events</h2>
<?php foreach ($featured_posts as $post) :
// Setup this post for WP functions (variable must be named $post).
setup_postdata($post); ?>
<?php if (get_post_status() == 'publish') : ?>
<a href="<?php the_permalink(); ?>" class="upcoming_event latest_box">
<p>
<time class="upcoming_event_date date_stamp"><?php the_field('event_date'); ?></time><br>
<span class="upcoming_event_title"><?php the_title(); ?></span>
</p>
</a>
<?php endif; ?>
<?php endforeach; ?>
<?php
// Reset the global post object so that the rest of the page works correctly.
wp_reset_postdata(); ?>
</div>
<?php endif; ?>
Wondering if there is a way to verify they are published before the loop and plug these into a variable? Sort of post status=> publish in an array then go if (!empty($featured_posts)) :, but that didn't seem to work out.
I have created a custom post type with custom fields(ACF) for patient galleries. To upload the images, I created a custom field type of "Gallery". Problem is, when I upload an image on one post, these images end up showing across every instance of the post type. I can create 20 different post types and they all have the same images, they are not exclusive to the post type I uploaded them to.
<div class="swiper-container gallery-top gallery-top-home">
<div class="swiper-wrapper">
<?php while ( $ba_loop->have_posts() ) : $ba_loop->the_post(); ?>
<?php
$images = get_sub_field('ba_photo');
?>
<?php foreach( $images as $image ): ?>
<div class="swiper-slide">
<img class="img-fluid" src="<?php echo $image['url']; ?>">
</div>
<?php endforeach; ?>
<?php endwhile; ?>
</div><!-- swiper wrapper end-->
</div><!-- swiper container end-->
I think first you need to get the id of the particular post..
Use this code..
$images = get_post_meta($post->ID, 'ba_photo', true);
instead of this
$images = get_sub_field('ba_photo');
The first code will differentiate every post by id, and you will get your required post details.
I have a custom post type called 'Projects' and I have added flexible field content to my single-projects.php. I'm trying to add a hero image for each project that sits in the main header.php as I want my hero image to sit behind the navigation. I have everything working but my hero image is displaying on CPT archive pages and CPT single pages but I just want to show the hero image on each project page.
in my header.php file I have:
<?php
// check if the flexible content field has rows of data
if( have_rows('project_flexible') ):
// loop through the rows of data
while ( have_rows('project_flexible') ) : the_row();
if( get_row_layout() == 'project_hero_image'):
$hero_image = get_sub_field('image'); ?>
<div class="hero-image"><img src="<?php echo $hero_image; ?>">
</div>
<?php
endif;
endwhile;
else :
// no layouts found
endif;
?>
maybe you could try this to only display on single page:
<?php
// check if the flexible content field has rows of data
if( have_rows('project_flexible') ):
// loop through the rows of data
while ( have_rows('project_flexible') ) : the_row();
if( get_row_layout() == 'project_hero_image'):
$hero_image = get_sub_field('image'); ?>
// Added this if statement
<?php if(is_single()){ ?>
<div class="hero-image"><img src="<?php echo $hero_image; ?>"></div>
<?php } ?>
<?php
endif;
endwhile;
else :
// no layouts found
endif;
?>
I created a group of fields in ACF plugin and this group belongs only to one page (for example page-contacts.php), but I want to display this group of field on several pages (for example page-main.php). Here is repeater field loop which is on the page-contact.php.
<?php if( have_rows('slider') ): ?>
<div id = "events-slider">
<?php while( have_rows('slider') ): the_row(); ?>
<div class = "events-slider__slide">
<img src="<?php the_sub_field('image'); ?>" alt>
</div>
<?php endwhile;?>
</div>
<?php endif;?>
How can I use this loop on page-main.php?
To get a field from another page, you can add the pages ID as a second parameter :
have_rows($field_name, $post_id);
Then you just need the post ID from the page, where the correct content is held -
Good luck!
I'm using Advanced Custom Fields on my website.
I have a repeater field called anime_par, with sub_field called animateur. Sub field animateur is a post-object.
I’m using this inside a loop in my page, a loop that displays posts from a category inside a custom post type.
What I’m trying to do is to display the post name and post link of the animateur selection inside my page.
Here is the code I’m using but it’s not working, it displays the permalink of my current page, not the one selected in the custom field.
<?php while(has_sub_field('anime_par')): ?>
<?php echo get_title('the_sub_field("animateur")'); ?>
<?php endwhile; ?>
Any suggestions to make this work?
thanks for your help,
This method is working for me, per the repeater and post object docs on ACF. You've got to set up the post object inside of the repeater loop.
I added in your field names, and some completely optional html to show the structure.
Hope it helps.
<!-- Start Repeater -->
<?php if( have_rows('anime_par')): // check for repeater fields ?>
<div class="a-container">
<?php while ( have_rows('anime_par')) : the_row(); // loop through the repeater fields ?>
<?php // set up post object
$post_object = get_sub_field('animateur');
if( $post_object ) :
$post = $post_object;
setup_postdata($post);
?>
<article class="your-post">
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
<?php // whatever post stuff you want goes here ?>
</article>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
<?php endwhile; ?>
</div>
<!-- End Repeater -->
<?php endif; ?>
the_sub_field doesn't work without has_sub_field
What you have to do it is use loop with has_sub_field as it said in the documenration http://www.advancedcustomfields.com/resources/functions/the_sub_field/
or you can use get_field('repeater_sluf') like that
$rows = get_field('repeater_field_name' ); // get all the rows
$first_row = $rows[0]; // get the first row
$first_row_image = $first_row['sub_field_name' ]; // get the sub field value
<?php if(get_field('favourite_design_quarters', 'user_'.$current_user->ID)): ?>
<?php while(has_sub_field('favourite_design_quarters', 'user_'.$current_user->ID)):
$company = get_sub_field('company_name');
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $company->ID ), 'package-thumbnail' );
?>
<tr>
<td><img src="<?php echo $image[0]; ?>" alt="<?=$company->post_title;?>" /></td>
<td><?=$company->ID;?></td>
<td style="text-align:left;"><?=$company->post_content;?></td>
<td><?=$company->post_date;?></td>
<td>Delete</td>
</tr>