Skip Navigation

[Resolved] A way to determine if a page that is hierarchical has pages under it

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to be able to use a conditional that tests whether a hierarchical page has child pages underneath it.

Solution: One way to approach this is to use a View of your CPT filtered by Post Parent (not Post Relationship), where the parent post is whatever parent post you want to test. Depending on how you have things set up, the parent post could be the current post, or the current post in the loop, or you could provide a post ID in a shortcode attribute, etc.

Then in the Loop Output area of this View, you can use the items found and no items found sections to include different content based on the number of results. If results are found, you know the current post has children. If not, you know the current post has no children.

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/
https://toolset.com/documentation/user-guides/digging-into-view-outputs/

This support ticket is created 6 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by nekhiaC 6 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#603901

Tell us what you are trying to do?

Is there any documentation that you are following?

https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/

I am wanting to do something like this:

function wpv_conditional_post_has_content($type, $object) {
$return = 0;
if ( $type == 'posts' ) {
if ( empty( $object->post_content ) ) {
$return = 0;
} else {
$return = count($object->post_content);
}
}
return $return;
}

so that I can do a conditional like this:

[wpv-conditional if="( wpv_conditional_post_has_content() eq '0' )"]
This post has no content[/wpv-conditional]

But instead, I want it to determine if the "post" has other posts underneath it from a hierarchical perspective (NOT a Types post relationship perspective). So essentially, does this page have pages organized underneath it.

Is there a similar example that we can see?

What is the link to your site?

#604137

Hi, one way to approach this is to use a View of your CPT filtered by Post Parent (not Post Relationship), where the parent post is whatever parent post you want to test. Depending on how you have things set up, the parent post could be the current post, or the current post in the loop, or you could provide a post ID in a shortcode attribute, etc.

Then in the Loop Output area of this View, you can use the items found and no items found sections to include different content based on the number of results. If results are found, you know the current post has children. If not, you know the current post has no children.

If this approach doesn't work for you, we can work on a conditional approach with shortcodes.

#604918

I got it working. Thanks for your help.