最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - Dont display post with no content

programmeradmin1浏览0评论

In my blog index page I do not want to show posts which has empty content but only title (How does filter the_posts work?) . I am looking at 'the_post' hook which seems to fire inside the loop . So , I thought if the action hook is fired inside the loop I could check the post content on 'the_post' hook and if it is null continue with the loop . I tried the below :

add_action('the_post' , 'rb_test_the_post');

function rb_test_the_post($post){
    if (($post->post_content) == '') { 
        continue;
    }
} 

This gives me an error in Wordpress ( "The site is experiencing technical difficulties " and also in my editor as it doesnt see any while or if statement for the continue statement.) . I am not able to understand why the continue statement doesnt work even though the hook is fired inside the loop .Could some one please explain?

Also , how would one achieve this (using a plugin and not the theme as I want the empty posts not to show even when I change theme) ?

In my blog index page I do not want to show posts which has empty content but only title (How does filter the_posts work?) . I am looking at 'the_post' hook which seems to fire inside the loop . So , I thought if the action hook is fired inside the loop I could check the post content on 'the_post' hook and if it is null continue with the loop . I tried the below :

add_action('the_post' , 'rb_test_the_post');

function rb_test_the_post($post){
    if (($post->post_content) == '') { 
        continue;
    }
} 

This gives me an error in Wordpress ( "The site is experiencing technical difficulties " and also in my editor as it doesnt see any while or if statement for the continue statement.) . I am not able to understand why the continue statement doesnt work even though the hook is fired inside the loop .Could some one please explain?

Also , how would one achieve this (using a plugin and not the theme as I want the empty posts not to show even when I change theme) ?

Share Improve this question edited Mar 11, 2020 at 15:15 Knownow asked Mar 11, 2020 at 14:13 KnownowKnownow 1216 bronze badges 5
  • You've changed from $post in the function call to $posts in the if() statement. As far as the function is concerned, $posts is an uninitialized variable, and you're using it as though it's an object. – Pat J Commented Mar 11, 2020 at 14:49
  • I changed the code in the if statement (which I happened to somehow mistype in SE) but the problem still persists . Get The site is experiencing technical difficulties error . – Knownow Commented Mar 11, 2020 at 15:17
  • I'd recommend turning on WP_DEBUG to see what the actual error is. The "WordPress is experiencing technical difficulties" error is a generic message that usually indicates there's a fatal error somewhere in your PHP code. – Pat J Commented Mar 11, 2020 at 16:46
  • 3 The error is simple your are trying to use continue not within a php loop but within a function, that will not work. – Tofandel Commented Mar 11, 2020 at 16:51
  • I feel so stupid thanks a lot . – Knownow Commented Mar 12, 2020 at 1:35
Add a comment  | 

3 Answers 3

Reset to default 0

The content is a property of the post object, not of the query object.
Use $post or get_post() :

  if( '' !== get_post()->post_content ) {
    // do something
    }

Just check the post content is empty or not:

if ( "" === $post->post_content )
{
//dont display your post 
 }
else
{
 //your post's code goes here
  the_title();
  the_content();
 }

I wanted to do this using a plugin and not theme . Here is how :

function my_filter_where($where = ''){
    return $where .= "AND trim(coalesce(post_content, '')) <>''";
}
add_filter('posts_where', 'my_filter_where');
发布评论

评论列表(0)

  1. 暂无评论