I've got an archive page for to display custom post types.

Within the WP_Query that displays the custom posts types I want to display an ACF (Advanced Custom Field) or, if the user hasn't filled out the ACF, then the title should appear.

I've tried this and the ACF field displays ok, but when it's not filled out, the title doesn't display, just the content of the post instead.

Here's the code I have (just for the title section):

<?php $loop = new WP_Query( array( 'post_type' => 'project', 'posts_per_page' => -1, 'orderby' => 'menu_order' ) ); ?>

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <div class="project col-md-4">
    <div class="col-xs-12 short-testimonial">

        <?php if(get_field('short_testimonial')): ?>

        <?php the_field('short_testimonial'); ?>

        <?php else: ?>

        <?php echo the_title(); ?>

        <?php endif; ?>


bumped to the homepage by Community yesterday

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

  • the_title(), just like the_field(), already echoes, so you don’t need the echo in front of it. Not sure if that’s the issue, but it’s something I noticed. – Jacob Peattie Apr 18 '18 at 13:25
  • Other than that though, the code looks fine. The only issue would be is if get_field('short_testimonial') isn't actually empty or false when it's not filled out. What do you see if you dump it with var_dump(get_field('short_testimonial'))? – Jacob Peattie Apr 18 '18 at 13:32

If in doubt, check the documentation first: https://www.advancedcustomfields.com/resources/get_field/

Check if value exists

This example shows how to check if a value exists for a field.

$value = get_field( 'text_field' );

if ( $value ) {
    echo $value;
} else {
    echo 'empty';

So, in for your case you would need to use:


$short_testimonial = get_field( 'short_testimonial' );

if ( $short_testimonial ) {
    echo $short_testimonial;
} else {


Also, you should note, as others have mentioned, that you don't need to echo the_title() as it echoes itself...

  • While it's probably better practice to code this way, the result of this code would be the same as the original code, given the same return value from get_field('short_testimonial'). – Jacob Peattie Apr 18 '18 at 13:44
  • @Mat thanks for the example, but I've tried it and I'm still getting the content instead of the title. Any other suggestions? – Andy Apr 18 '18 at 15:05

You should try removing "echo" from "the_title()".

This should work

<div class="project col-md-4">
     <div class="col-xs-12 short-testimonial">
               if( get_field( 'short_testimonial' ) ): 
               the_field( 'short_testimonial' );

You could also look at the official documentation here.

You will notice that the_title already displays it by default.

<?php the_title( $before, $after, $echo ); ?>
  • Check the documentation - advancedcustomfields.com/resources/get_field – Mat Apr 18 '18 at 13:41
  • While echo doesn't need to be there, it wouldn't cause this problem. – Jacob Peattie Apr 18 '18 at 13:42
  • @Mat, you don't need to add "get_field" to a variable before checking it on a statment – danrodrigues Apr 18 '18 at 13:44
  • @JacobPeattie you're right... but I've had experiences in the past where removing echo solved the problem. So it is worth a try :) – danrodrigues Apr 18 '18 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.