wp query - How to give classname to post if post has no content?

admin2025-06-05  1

I want to give a post a classname on the homepage (index) if the post on the single page has no content. So when the post itself exists but there is no content, I want to give a classname to that specific post/article which will be shown on the homepage.

My question: can this be done? If so, how?

I want to give a post a classname on the homepage (index) if the post on the single page has no content. So when the post itself exists but there is no content, I want to give a classname to that specific post/article which will be shown on the homepage.

My question: can this be done? If so, how?

Share Improve this question asked Dec 28, 2018 at 19:12 SiyahSiyah 1932 silver badges12 bronze badges 1
  • 1 Are you using the post_class function in your template? How are you showing posts on the homepage? There's no code in your question :/ Also what are you trying to do that requires this? If the goal is to apply CSS rules, you can already style empty containers with the ::empty pseudo selector – Tom J Nowell Commented Dec 28, 2018 at 20:46
Add a comment  | 

2 Answers 2

Reset to default 1

As Tom J Nowell mentioned in his comment, it is not clear what do you want to accomplish. If you want to just show your empty content div, you can use CSS as follows:

div:empty {
    width: 100%;
    height: 20px;
    margin-bottom: 10px;
    background: #ff0000;
}

It will show your empty content div as a nice, red band.

If you want to add a post class programatically to the posts that have no content you should use the following snippet inside the loop:

if ( empty( get_the_content() ) ) {}

If you're using it outside the loop to call other posts you can use:

<?php if (empty(get_post_field('post_content', $MY_POST_ID))): ?>

<?php endif ?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749061469a316022.html

最新回复(0)