wp query - has_excerpt() not working inside wp_query

I am trying to check for an excerpt and if it doesn't exist then show the content but trimmed. Currently nothing is

I am trying to check for an excerpt and if it doesn't exist then show the content but trimmed. Currently nothing is showing.

<?php if (has_excerpt() ): ?>
    <p><?php echo get_the_excerpt(); ?></p>
<?php else: ?>
    <p><?php echo wp_trim_words(get_the_content(), 25); ?></p>
<?php endif; ?>

However, if I simply do the below then I see content. Is there an issue with my above code? This is running inside a WP_Query loop

<p><?php echo wp_trim_words(get_the_content(), 25); ?></p>

The loop:

        <?php $args = array(
                'post_type'        => 'post',
                'posts_per_page'   => 8,
                'category__not_in' => 4
            );
            $latestPosts = new WP_Query( $args );
        ?>
        <?php if ($latestPosts->have_posts() ): ?>
        <ul>
            <?php while ($latestPosts->have_posts() ): $latestPosts->the_post(); ?>
            <li>
           //The excerpt code etc. goes here
           </li>
            <?php endwhile; ?>
            <?php wp_reset_postdata(); ?>
        </ul>
       <?php endif; ?>

I am trying to check for an excerpt and if it doesn't exist then show the content but trimmed. Currently nothing is showing.

<?php if (has_excerpt() ): ?>
    <p><?php echo get_the_excerpt(); ?></p>
<?php else: ?>
    <p><?php echo wp_trim_words(get_the_content(), 25); ?></p>
<?php endif; ?>

However, if I simply do the below then I see content. Is there an issue with my above code? This is running inside a WP_Query loop

<p><?php echo wp_trim_words(get_the_content(), 25); ?></p>

The loop:

        <?php $args = array(
                'post_type'        => 'post',
                'posts_per_page'   => 8,
                'category__not_in' => 4
            );
            $latestPosts = new WP_Query( $args );
        ?>
        <?php if ($latestPosts->have_posts() ): ?>
        <ul>
            <?php while ($latestPosts->have_posts() ): $latestPosts->the_post(); ?>
            <li>
           //The excerpt code etc. goes here
           </li>
            <?php endwhile; ?>
            <?php wp_reset_postdata(); ?>
        </ul>
       <?php endif; ?>
Share Improve this question edited Jul 2, 2020 at 10:24 Iggy's Pop asked Jul 2, 2020 at 10:03 Iggy's PopIggy's Pop 1151 gold badge2 silver badges8 bronze badges 11
  • Can we see the entire WP_Query loop? – Tom J Nowell Commented Jul 2, 2020 at 10:22
  • @TomJNowell, have updated question – Iggy's Pop Commented Jul 2, 2020 at 10:25
  • 1 How about replacing the echo 'has excerpt' with var_dump( get_post_field( 'post_excerpt' ) ) - what's the output? Maybe the excerpt is a space? – Sally CJ Commented Jul 2, 2020 at 11:46
  • 1 Well then, no wonder. That is considered a valid excerpt because its length is > 0. I.e. empty( ' ' ) = false. So you should replace the value in the database. – Sally CJ Commented Jul 2, 2020 at 11:54
  • 1 Either way, thanks so much. This was doing my head in and you resolved it. Would award you accepted answer but you haven't posted one yet, only comments. – Iggy's Pop Commented Jul 2, 2020 at 12:01
 |  Show 6 more comments

2 Answers 2

Reset to default 1

So we already resolved the issue via the comments, but I thought I should explain something:

has_excerpt() returns ! empty( $post->post_excerpt ) if the post has a custom/manual excerpt, so if the function is returning true when it should be false, then it's likely because the length of the excerpt in the database is > 0, e.g. when the value is a whitespace — empty( ' ' ) = false. (But note that empty( '0' ) or empty( 0 ) is true)

So if that's the case, then you should fix the post_excerpt value in the database so that the function returns the expected value.

But as I said in the comment, I'm not sure why the excerpt was imported with all that whitespace, so I could only guess they were added during export or maybe the import, via a filter.

Anyway, I hope this answer helps! :)

Iggy!

With WP functions, typically speaking the get_ before the function signifies that the call is made outside of the loop, and as such you need to declare an object ID within the brackets, like so:

get_the_excerpt( $post->ID )

If you're using this function within the object loop, you should just call:

the_excerpt()

Which doesn't require the object ID, because that is already making up the query.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742298354a4417572.html

相关推荐

  • wp query - has_excerpt() not working inside wp_query

    I am trying to check for an excerpt and if it doesn't exist then show the content but trimmed. Currently nothing is

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信