X

How to Check if Post has the_excerpt in WordPress

Snippets by IsItWP

Are you looking for a way to check if a post contains an excerpt? While there’s probably a plugin for this, we have created a quick code snippet that you can use to check if post has the_excerpt function in WordPress.

WordPress will use the post as an excerpt when one does not exists, so this is a great way to display the excerpt when not every post will contain one.

Instructions:

All you have to do is add this code to your theme’s post.php file:

<?php
    $my_excerpt = get_the_excerpt();
     if($my_excerpt !='') {
         the_excerpt();
     } 
?>

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 21 best WordPress real estate themes and how to start a blog.

Comments   Leave a Reply

  1. i have moddriod theme installed on my website but some posts are showing excerpts and some are picking random texts and tags from the post, some of them only show the title which is not attractive as well as acceptable. will it work if I follow your instructions? I am not sure about the problem source
    1. is it in the theme
    2. rank math plugin
    or anything else please help me.

    1. Hey Kishor, you can troubleshoot by disabling the plugins one by one and see if any plugin is causing the issue.

  2. Tweets that mention Wordpress Check it post has the_excerpt – wpsnipp.com Wordpress code snippets for your blog -- Topsy.com January 31, 2011 at 7:44 pm

    […] This post was mentioned on Twitter by HN Firehose, wp_freak. wp_freak said: #wordpress Check it post has the_excerpt http://bit.ly/h8VGwl […]

  3. WordPress Check it post has the_excerpt – wpsnipp.com WordPress code snippets for your blog…

    Adding this PHP code to your wordpress theme will allow you to check if a post contains an excerpt. WordPress will use the_content as an excerpt when one does not exists so this is a great way to display the excerpt when not every post will contain one…

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!