Reputation: 164
I'm working on a site whereby there is a simple template for a certain page in the site.
The page in question is an About Us page.
On the page there is the first main WordPress loop which just spits out the standard content of the main Wysiwyg field.
Underneath that the template has some code which spits out a loop of a custom post type called Staff. This custom post type has information on each staff member and each one is spat out there.
What I have added to the page via the backend is an Advanced Custom Field using ACF Pro. The field is called Our Story (our_story) and I'd like to be able to spit that field out on the template underneath all the staff members.
If I place the following code :
<p><?php the_field('our_story') ?></p>
into the template in the main WordPress loop which is at the start of the template then it shows up fine but I effectively want to start the same loop again but underneath the loops which have spat out the staff members.
If I copy the exact same loop which is at the start of the template to the end of the template then I don't get anything output on the page where that code is. Do I therefore need to reset the code or something or is there an easy way of perhaps caching field content and placing it elsewhere in the template?
Sorry entire template code shown here :
<?php
/*
Template Name: About (Staff Listing)
*/
?>
<?php get_header(); ?>
<div class="page-wrap_content">
<div class="center">
<div class="row">
<section class="span8">
<h1><?php the_title(); ?></h1>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'Yes'
)
)
);
$wp_query = new WP_Query( $args );
?>
<?php if (have_posts()) : ?>
<h2>Our Team</h2>
<div>
<?php while (have_posts()) : the_post(); ?>
<article class="service">
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail('service_thumb'); ?></a>
<h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
<!-- <p class="core">Core Staff</p> -->
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo '<a href="/what-we-do/'.$service.'">'.$field['choices'][ $service ].'</a> ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p><a href="<?php the_permalink() ?>" class="boldlink">Read more »</a></p>
</article>
<?php endwhile; ?>
</div>
<?php else : ?>
<p>No core staff found</p>
<?php endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'No'
)
)
);
$wp_query = new WP_Query( $args );
?>
<?php if (have_posts()) : ?>
<h2>Experts</h2>
<div>
<?php while (have_posts()) : the_post(); ?>
<article class="service">
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail('service_thumb'); ?></a>
<h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo '<a href="/what-we-do/'.$service.'">'.$field['choices'][ $service ].'</a> ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p><a href="<?php the_permalink() ?>" class="boldlink">Read more »</a></p>
</article>
<?php endwhile; ?>
</div>
<?php else : ?>
<p>No experts found</p>
<?php endif; ?>
<?php the_field('our_story', '710'); ?>
</section>
<?php get_sidebar(); ?>
</div>
</div>
</div>
<?php get_footer(); ?>
Upvotes: 0
Views: 83
Reputation: 2039
I've adjusted your template to include named WP_Query()
loops, and apply wp_reset_postdata()
after each custom loop. This should allow the final the_field()
call to have access to the current post ID, which is what Advanced custom fields needs in order to fetch the post metadata for the current post.
Behind the scenes, ACF runs (int) get_the_ID();
to get the ID of the current post. If your custom query loops are not reset, this won't work.
<?php
/*
Template Name: About (Staff Listing)
*/
?>
<?php get_header(); ?>
<div class="page-wrap_content">
<div class="center">
<div class="row">
<section class="span8">
<h1><?php the_title(); ?></h1>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'Yes'
)
)
);
// Specify the custom query, so you can build a custom loop.
// ---------------------------------------------------------
$staff_query = new WP_Query( $args );
?>
<?php if ($staff_query->have_posts()) : ?>
<h2>Our Team</h2>
<div>
<?php while ($staff_query->have_posts()): ?>
<?php
// Set up the post to allow use of template tags
// -------------------------------------------------
$staff_query->the_post();
?>
<article class="service">
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail('service_thumb'); ?></a>
<h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
<!-- <p class="core">Core Staff</p> -->
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo '<a href="/what-we-do/'.$service.'">'.$field['choices'][ $service ].'</a> ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p><a href="<?php the_permalink() ?>" class="boldlink">Read more »</a></p>
</article>
<?php endwhile; ?>
<?php
// Need to reset postdata!
// -----------------------------------------------------
wp_reset_postdata();
?>
</div>
<?php else : ?>
<p>No core staff found</p>
<?php endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'No'
)
)
);
$expert_query = new WP_Query( $args );
?>
<?php if ($expert_query->have_posts()) : ?>
<h2>Experts</h2>
<div>
<?php while ($expert_query->have_posts()): ?>
<?php
// Set up post
// -------------------------------------------------
$expert_query->the_post();
?>
<article class="service">
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail('service_thumb'); ?></a>
<h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo '<a href="/what-we-do/'.$service.'">'.$field['choices'][ $service ].'</a> ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p><a href="<?php the_permalink() ?>" class="boldlink">Read more »</a></p>
</article>
<?php endwhile; ?>
<?php
// Need to reset postdata!
// -----------------------------------------------------
wp_reset_postdata();
?>
</div>
<?php else : ?>
<p>No experts found</p>
<?php endif; ?>
<?php
// this should work now:
the_field('our_story');
//the_field('our_story', '710');
?>
</section>
<?php get_sidebar(); ?>
</div>
</div>
</div>
<?php get_footer(); ?>
I think it's easy to get confused with custom loop logic when it's all handled within a template - I prefer to manage post data for custom loops by means of a function or class method which returns an array of post data - you can then loop over this in your template.
Hope this helps.
Resources: https://codex.wordpress.org/Class_Reference/WP_Query#Multiple_Loops
Upvotes: 1
Reputation: 65274
it's because you need a post id to get the field. By default it is the current post's id.
try this: <p><?php the_field('our_story', xxx) ?></p>
where xxx is the post id of About Us.
Upvotes: 1