Note: This article is for users of Views, an awesome WordPress plugin that makes it ridiculously easy to pull info from WordPress standard and custom fields and display them exactly how you want it.
In one of the projects I am working on, the requirement is to display a ‘More info’ link for every entry on a listing page having content. If there is no content for an entry, ‘More info’ link should not appear. It does not make sense to provide a link to the single entry if it has nothing additional to show to the visitor, isn’t it?
While it is straight-forward to check whether custom fields values are empty or not, it’s not so w.r.t post body.
Here’s how post body can be tested for presence of content:
Add the following in child theme’s functions.php:
add_shortcode ( 'wpv-post-getcontent', 'wpv_getcontent' );
function wpv_getcontent() {
global $post;
if($post->post_content != '') :
return 1;
else :
return 0;
endif;
}
Then use [[wpv-if]] shortcode and check for the value of [wpv-post-getcontent]. If the entry has content it will be equal to 1, otherwise 0.
Sample code in View Template:
Source: http://wp-types.com/forums/topic/testing-for-empty-post-body/
Thank you so very much! This works perfectly.