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 or in a site-specific plugin:

<?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. 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 […]

  2. 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!