admin管理员组文章数量:1314471
all I want is to check if the custom post item in the loop has a parent. I am using this inside a shortcode. I could not find how to access the property from inside the loop.
Thank you my dears
$q = new WP_Query(array(
'post_type' => 'my_custom_post_type',
'posts_per_page' => 5,
'category' => '',
));
while ($q->have_posts()) {
$q->the_post();
... <- Check if the loop item has a parent
}
wp_reset_postdata();
all I want is to check if the custom post item in the loop has a parent. I am using this inside a shortcode. I could not find how to access the property from inside the loop.
Thank you my dears
$q = new WP_Query(array(
'post_type' => 'my_custom_post_type',
'posts_per_page' => 5,
'category' => '',
));
while ($q->have_posts()) {
$q->the_post();
... <- Check if the loop item has a parent
}
wp_reset_postdata();
Share
Improve this question
asked Nov 26, 2020 at 14:55
El GringoEl Gringo
31 bronze badge
1 Answer
Reset to default 2The WP_Post
class has an attribute post_parent
that you can use for this.
while ($q->have_posts()) {
$q->the_post();
$post = get_post();
if (!empty($post->post_parent)) {
// do something different
}
}
本文标签: custom post typesChecking if looped item has a parent inside a shortcode
版权声明:本文标题:custom post types - Checking if looped item has a parent inside a shortcode 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741961741a2407318.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论