Aleš Sýkora wrote

Conditional check if page is parent or child in WordPress

14. 1. 2021 in

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";
}

?>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

More from category
Aleš Sýkora wrote
Speeding up WordPress & Toolset
7. 11. 2019 in
Tips for speeding up WP + Toolset.
Read More
Aleš Sýkora wrote
Astra Theme Container Width Fix
4. 9. 2019 in
If you encounter some issues with container width on mobile device when using Astra Theme and Toolset and Bootstrap try this CSS fixes.
Read More
Aleš Sýkora wrote
Emoji deleted from ACF field after saving
2. 4. 2021 in
So you have a ACF text field and you are trying to insert emoji to it. For example as a category icon. You add the field, add the emoji, use the PHP code to display the field. And emoji is missing. After that you found that every time you save the page, emoji dissapear from […]
Read More
magnifiercross linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram