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!
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'm creating a custom WP theme - within the site, there is an employee section. Using 'Advanced Custom Fields' repeater, I've made it possible for WP users to go to a page and add/change/delete employee members.
I'm wanting this employee section to be added to other places on the website, but only needs to be updated in one place - rather than having to go into several pages to make the same changes.
I'm relatively new to WP dev and PHP, but here's what I've tried:
I've created a new php file with only the employee section:
<?php /* Template Name: StaffSection */ ?>
<h1>Testing</h1><!-- This line runs fine -->
<?php<!-- None of this runs -->
// check if the repeater field has rows of data
if( have_rows('employees') ):
// loop through the rows of data
while ( have_rows('employees') ) : the_row(); ?>
<div class="col-lg-3 col-md-6 gap">
<a href="<?php the_sub_field('employee-link'); ?>">
<img class="leadership-img" src="<?php the_sub_field('employee-image'); ?>">
<h4 class="position"><?php the_sub_field('employee-name'); ?></h4>
</a>
<p class="position"><?php the_sub_field('employee-title'); ?></p>
</div>
<?php endwhile;
else :
// no rows found
endif; ?>
On the page that I'm wanting to 'include' this section on:
<section id="leadership" class="section">
<div class="container-fluid">
<div class="wrapper">
<div class="row leadership-section">
<?php include 'staff-section.php'; ?>
</div>
</div>
</div>
</section>
Within WP, I've created a new WP page and linked it to the 'StaffSection' template that I created. I have 'Advanced Custom Fields' on that page to pull content that WP users define.
I know the 'include' function is working with that test h1 tag, but any idea why it isn't reading the php repeater loop below that?
It could be that if ( have_rows('employees') )... etc etc is returning false because there is no 'employees' repeater that belongs to the post object defined within the loop.
One solution I use to make a field that is displayed across many pages is to create a secondary query to retrieve the repeater.
For instance, we can do this.
1. create a post with the category 'employees'
2. Go to ACF and set the logic so the repeater only appears on posts with category 'employees'
3. Query post object with category 'employees'
4. Access repeater from within the query
<?php
$repeater_query = new WP_Query(array('category_name' => 'employees'))
if ($repeater_query->have_posts() ) {
while ($repeater_query->have_posts() ) {
$repeater_query->the_post();
// check if the repeater field has rows of data
if( have_rows('employees') ):
// loop through the rows of data
while ( have_rows('employees') ) : the_row(); ?>
<div class="col-lg-3 col-md-6 gap">
<a href="<?php the_sub_field('employee-link'); ?>">
<img class="leadership-img" src="<?php the_sub_field('employee-image'); ?>">
<h4 class="position"><?php the_sub_field('employee-name'); ?></h4>
</a>
<p class="position"><?php the_sub_field('employee-title'); ?></p>
</div>
<?php endwhile;
else :
// no rows found
endif;
}
} wp_reset_postdata();
I hope this helps. Cheers
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>
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.
I am currently developing a mutlisite wordpress setup, each site is a different language (EG: site.com, site.dk etc).
The sites consist of a number of pages which contain static content, however I also want to include posts (a blog) into both of the sites.
Q1. Is it possible to create a page that shows all the posts listed by latest publish date, with a dropdown that filters by category? How do I do this? Do I need to refer to loop.php?
Basically it should return the following code for all post articles...
<article class="post">
<a href="<URL Link to Post Article>" rel="bookmark">
<figure>
<img title="<Post Title>" alt="<Post Title>" src="<http://url/PostImage.jpg>" width="900" height="600" />
</figure>
<div class="cover">
<h2>Post Title</h2>
<time pubdate="2013-03-27T21:09:59+00:00">November 18, 2012</time>
</div>
</a>
</article>
Why doesn't this work? It doesn't return anything?
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<!-- article -->
<article class="post">
<?php get_posts(); ?>
<div id="grid-switcher">
featured
latest
</div>
<div id="view-blocks">
<div id="latest-post" class="post-grid active">
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail(array(250,250)); // Declare pixel size you need inside the array ?>
<?php endif; ?>
<!-- /post thumbnail -->
<div class="cover">
<h2><?php the_title(); ?></h2>
<time pubdate="<?php the_date(); ?>"><?php the_date('Y-m-d', '<h2>', '</h2>'); ?></time>
</div>
</a>
I have already created a page template portfolio-page.php for the above but cannot findout how to loop through the posts and return them with the above code?
Q2. How do I return a dropdown list with all the categories?
Q3. How do I filter by category from the dropdown list?
Thanks for any help! :)
Sorry I am new to PHP and wordpress...
The question is a bit vague so I will just share some starting points:
A1) To get a list of posts you can use the get_posts function from wordpress. It already sorts them by the post date desc and you can also add a parameter for the category if one is picked.
A2) To get a dropdown of categories there's a function that does exactly that: wp_dropdown_categories
Check the docs as there are a lot of parameters that can be used with the get_posts function so you should be able to filter them as you wish.
I think its a good idea for you to check the database you have wordpress running on.
if you check the wp_posts table you can see the differend fields that you can filter on.
You will be better off using custom query's to filter te posts and order them