Aleš Sýkora / November 28, 2023 / 0 comments
Check if current post has child in WordPress
1 min read / Custom Code, Oxygen Builder, Plugins, WordPress / Share on: Twitter, LinkedIn, Facebook
Post summary: When you are in situation, where you want to check if current post has a child posts, then you need to add custom function. It’s the easiest way how to do it. Check if post has childs programatically You can add this function to your code snippets plugin or custom funcionality plugin (I am using…
When you are in situation, where you want to check if current post has a child posts, then you need to add custom function. It’s the easiest way how to do it.
Check if post has childs programatically
<?php
function great_tit_babysitter()
{
global $post; //load current post variables
return count(
get_posts(
array(
'post_parent' => $post->ID,
'post_type' => $post->post_type
)
)
);
}
You can add this function to your code snippets plugin or custom funcionality plugin (I am using Advanced Scripts plugin).
After that, you can finally go to Oxygen Builder and use the function. I will use it for a conditional. So click to create a conditional > select a PHP Function Return Value > fill the function name.
I am using this function to conditionally display section with child posts of current post. The complete condition in Oxygen looks like this:
Fuel my passion for writing with a beer🍺
Your support not only makes me drunk but also greatly motivates me to continue creating content that helps. Cheers to more discoveries and shared success. 🍻