I’ve spent all afternoon trying to display custom field values in a page template, first with Types (which is awful) and now with ACF. I don’t know what I’m doing wrong here…
This is my custom loop in a page template with two of my ACF fields:
<div id="reviews">
<h2>User Feedback About the Animas</h2>
<?php
$args = array(
'post_type' => 'review',
'posts_per_page' => 10
);
$loop = new WP_Query( $args );
echo '<ul>';
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<?php the_content(); ?>
<h5 class="author"><?php the_title(); ?></h5>
<p><?php the_field('your_name'); ?></p>
<p><?php the_field('your_testimonial'); ?></p>
</li>
<?php endwhile;
echo '</ul>'; ?>
<?php wp_reset_query(); ?>
</div>
The ACF fields don’t display at all. Please help, I’ve looked at dozens of code examples and nothing I’ve tried today has worked.
Whilst certainly no expert, I couldn’t get the_field to work in a WP-Query, so I used this instead:
$name = get_post_meta(get_the_ID() , 'your_name', true); //custom field
then just echo $name
This still doesn’t work for me… no custom field is displayed 🙁
I tried it in a new empty page template, in or out of the loop, same result… nothing displayed.
Never got it working as above, but figured out I didn’t need ACF after all. Now it’s working perfectly, just using Gravity Forms + Custom Post Types.
Try that to test what happens:
$yourname = get_field('your_name');
var_dump($your_name);
var_dump show you, what returned by get_field/the_field function.
to hide your tests from users, put testing code into that check:
if ( get_current_user_id() == 1 ) { /* if your user_id isn't 1, change 1 into code to your user_id */
//your testing code here
}