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?
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 ?>
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