Aleš Sýkora / January 14, 2021 / 2 comments

Conditional check if page is parent or child in WordPress

Post summary: If you need to display something only if post has childs (or not), you can use this conditional in your code.

If you need to display something only if post has childs (or not), you can use this conditional in your code.

<?php


$args = array(
    'post_parent' => get_the_ID(), // Current post's ID
);
$children = get_children( $args );
// Check if the post has any child
if ( ! empty($children) ) {
    // The post has at least one child
  echo "has childs";
} else {
    // There is no child for this post
    echo "do not have childs";
}

?>

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. 🍻

2 comments

  • This old snippet was handy today when adding a conditional display of a list of subpages (using the Page-list plugin shortcodes) within Bricks Builder!

    Thanks heaps ?

  • A

    Great to hear it helps! 🙂

Share Your Thoughts