wp query - How do I show the post title if an advanced custom field hasn't been used?

admin2025-01-07  3

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; ?>

    </div>

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; ?>

    </div>
Share Improve this question asked Apr 18, 2018 at 13:14 AndyAndy 112 silver badges4 bronze badges 2
  • 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 Commented Apr 18, 2018 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 Commented Apr 18, 2018 at 13:32
Add a comment  | 

2 Answers 2

Reset to default 0

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:

<?php

$short_testimonial = get_field( 'short_testimonial' );

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

?>

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

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

This should work

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

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 ); ?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736263606a904.html

最新回复(0)