Skip Navigation

[Resolved] I need to only display parent posts that actually have children

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.

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

Problem:
Trying to create a page that Lists only the parent posts that actually have Children, not all the parent posts.

Solution:
1. Add this code in your theme’s or child theme’s functions.php file:

function child_exists_func( $atts ){
    extract( shortcode_atts( array(
        'child_post_type_slug' => '',
    ), $atts ) );
    $child_posts = types_child_posts($child_post_type_slug);
    if ($child_posts) {
        return true;
    }
}
 
add_shortcode( 'child-exists', 'child_exists_func' );

2. Register the ‘child-exists’ shortcode first in Toolset >> Settings >> Front-end Content >> Third-party shortcode arguments.

3. Then add this shortcode in your View >> Loop Output Editor:

 [wpv-conditional if="( '[child-exists child_post_type_slug=belong]' eq '1' )"]
[wpv-post-link]
[/wpv-conditional] 

==> Where as “belong” is your child post type slug that you should replace.

This support ticket is created 6 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 15 replies, has 3 voices.

Last updated by Noman 6 years, 7 months ago.

Assisted by: Noman.

Author
Posts
#555527

Tell us what you are trying to do?
I'm trying to create a page that lists only the parent posts that actually have children, not all the parent posts.
The code for my template of this view looks like this:

<h3>[wpv-post-field name='wpsl_city']</h3>
<h4>[wpv-post-title]</h4>
[wpv-view name="view"]

I know I need to wrap this in a conditional of some kind, but I just cannot get it working....

Is there any documentation that you are following?
I have looked at a number of support threads and think that this one is the closest to what I need
https://toolset.com/forums/topic/conditional-html-check-if-post-has-children/
, but as soon as I apply the conditional my parent post title no longer displays, just the child posts.

Is there a similar example that we can see?

What is the link to your site?
The current page I am trying to create is here:
hidden link

#555529

Loop output editor

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<wpv-loop>
		[wpv-post-body view_template="city-shop-eggsperts"]
	</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

Template for this view:

<h3>[wpv-post-field name='wpsl_city']</h3>
<h4>[wpv-post-title]</h4>
[wpv-view name="view"]
#555636

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Martin,

Thank you for contacting Toolset support. I am working on this right now and I will update you with my findings shortly. If it can’t be done using View shortcodes, then I might need to add some small code to achieve this. Please wait and I will update you accordingly.

Thank you

#555644

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Register Custom Shortcode.png

1. I have created a custom shortcode. Please add this code in your theme’s or child theme’s functions.php file:

function child_exists_func( $atts ){
	extract( shortcode_atts( array(
        'child_post_type_slug' => '',
    ), $atts ) );
	$child_posts = types_child_posts($child_post_type_slug);
	if ($child_posts) {
		return true;
	}
}

add_shortcode( 'child-exists', 'child_exists_func' );

2. Register the ‘child-exists’ shortcode first in Toolset >> Settings >> Front-end Content >> Third-party shortcode arguments. See attached screenshot.

3. Then add this shortcode in your View >> Loop Output Editor:

[wpv-conditional if="( '[child-exists child_post_type_slug=belong]' eq '1' )"]
[wpv-post-link]
[/wpv-conditional]

Where as “belong” is your child post type slug that you need to replace.

I hope it helps, Thank you

#556377

So I have done what you said. My loop output editor now looks like this:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<wpv-loop>

[wpv-conditional if="( '[child-exists child_post_type_slug=belong]' eq '1' )"]		
      [wpv-post-body view_template="city-shop-eggsperts"]
[/wpv-conditional]
	</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

The condititional is obviously not evaluating to true as no content is now displaying if you examine:
hidden link

#556460

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello Martin,

I need to take a look at your setup and to request temporary website WP-admin and FTP info for your site to debug further and to help in resolving this. Your next answer will be private which means only you and I have access to it.

=== Please backup your database and website ===

✙ I would additionally need your permission to de-activate and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important.

✙ Please add the Link to the [View] Edit Screen.

✙ What is the expected output on the front page.

Thank you

#557149

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for providing login details. I am working on this right now and I will update you with my findings shortly.

Thank you for your patience.

#557263

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello Martin,

I added the code in functions.php of the theme, made some edits. Its working good now, please check here: hidden link

Thank you

#557366

Hi Noman, would you mind to updated your code in the forum. Thanks.

#557391

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello williamm-5 ,

The code and steps were same as mentioned here:
https://toolset.com/forums/topic/i-need-to-only-display-parent-posts-that-actually-have-children/#post-555644

All you have to make sure is:
- Change the “belong” to your child post type slug with your child post slug / name.
- Make sure you have registered the given shortcode first.
- And make sure to add it in active theme's functions.php file.

No other changes were made. Thank you

#557418

hi, Noman,

when i use following code in the view

{
[child-exists child_post_type_slug=belong]
[wpv-conditional if="( '[child-exists child_post_type_slug=belong]' eq '1' )"]      
      [wpv-post-link]
[/wpv-conditional]
}

[child-exists child_post_type_slug=belong] did have an output "1".

but condition output of [wpv-post-link] is nothing.

Why ?

Thanks.

#557422

Noman, it works, Thanks very much.

#558064

Thanks very much for your help. All fixed!

Just a question though as it would be good to know exactly what you had to change.

1. What changes did you make to the functions.php file, if any?
2. What changes did you make to the conditional, if any?
3. Did you have to change anything else in the way that I had it configured?

Martin

#558089

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Martin,

1. What changes did you make to the functions.php file, if any?
==> Nothing, just added the code in functions.php file, as I saw it was not there.

2. What changes did you make to the conditional, if any?
==> Just changed, the “belong” child post type slug with your child post slug / name like this:

[wpv-conditional if="( '[child-exists child_post_type_slug=eggspert]' eq '1' )"]		
      [wpv-post-body view_template="city-shop-eggsperts"]
[/wpv-conditional]

3. Did you have to change anything else in the way that I had it configured?
==> No other changes.

#559817

I've tried to recreate a live version of the view here: hidden link

I have used exactly the same process outlined above, but it is not showing any content. I know that I have probably missed a simple and obvious step.

I'd really appreciate it if you could use the same credentials supplied about to ascertain what I have done wrong this time.

Thanks
Martin

The forum ‘Types Community Support’ is closed to new topics and replies.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.