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.
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.
Hey Kishor, you can troubleshoot by disabling the plugins one by one and see if any plugin is causing the issue.
[…] 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 […]
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…