Connect with us

Tips & Tricks

Display the_excerpt only if there is text

Have you ever wanted to display the excerpt only if you write one? A simple couple lines of code can display the_excerpt any where you like.

In my new theme, I am using this coded trick to display the excerpt on a single post only if I’ve got text inside. Usually if you use the_excerpt and you don’t have one, it will fake one for you.

This is not something that I wanted to do on the single post page. So I used the following code to check if the excerpt existed.

if ( !empty( $post->post_excerpt ) ) :

Once this action is taken into account, you can factor in what code you want to out put if the post_excerpt isn’t empty.

if ( !empty( $post->post_excerpt ) ) :
	the_excerpt();
else :
	false;
endif;

The above code checks if there is an excerpt and print’s it to the screen. If there isn’t an except, it doesn’t do anything.

2 Comments

2 Comments

  1. Tim

    May 17, 2009 at 11:39 am

    Very helpful tip. I love the flexibility of WordPress and this is one more trick to have when working out a custom content display.
    Thanks for posting this!

    Tim

    Tim´s last blog post..Disappearing Widgets – A Solution in WordPress 2.7

  2. Andrew

    September 17, 2009 at 6:10 am

    I’m trying to get this to work only
    $returns = mysql_query($query);
    $result = mysql_fetch_row($returns);
    $post_title = $result[5];
    $post_excerpt = $result[7];

    display 2 titles and not the main decription or article body, any help please?
    Andrew

You must be logged in to post a comment Login

Random Search Terms

Title

Top Articles

powered by WassUp

Recent Posts: Fully Net Worth . com

Bill Aydin Net Worth- How Rich is Jennifer Aydin’s Husband?

Bill Aydin Net Worth- How Rich is Jennifer Aydin’s Husband?

Dr. Nebil Bill Aydin is a Double Board Certified Plastic and Reconstructive Surgeon. His specialization lies in aesthetic cosmetic surgery of the face and body. Aydin is originally from Istanbul, Turkey but had migrated to Los Angeles in the U.S. when he was 16. As of 2019, Bill Aydin net worth is estimated to be […]

Dakotaz Net Worth: How Rich is the Twitch Streamer Actually?

Dakotaz Net Worth: How Rich is the Twitch Streamer Actually?

Brett Hoffman, who is better known by his online alias as Dakotaz, sometimes referred to as DK, is a Twitch Streamer and a Youtuber. He is an active player of the game Fortnite and today has over 4 million followers on his Twitch channel. As of 2019, Dakotaz Net Worth is estimated to be $2.5 […]

Ben Zobrist Net Worth: 4 Interesting Facts You Should Know

Ben Zobrist Net Worth: 4 Interesting Facts You Should Know

Ben Zobrist is an American professional baseball second baseman and outfielder who currently plays for the Chicago Cubs of Major League Baseball. Early Life Ben was born on May 26, 1981, in Eureka, Illinois to Cynthia and Tom Zobrist. He started playing baseball when he was eight years old. He graduated from Eureka High School […]

%d bloggers like this: