admin管理员组文章数量:1130349
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 |2 Answers
Reset to default 1As 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 ?>
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_classfunction 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::emptypseudo selector – Tom J Nowell ♦ Commented Dec 28, 2018 at 20:46
2 Answers
Reset to default 1As 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 ?>
本文标签: wp queryHow to give classname to post if post has no content
版权声明:本文标题:wp query - How to give classname to post if post has no content? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749061444a2310165.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


post_classfunction 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::emptypseudo selector – Tom J Nowell ♦ Commented Dec 28, 2018 at 20:46