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

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.

You are not logged in. You can view support threads, but not post.
If you are already logged in, please refresh your browser.

Related documentation: Creating Post Type Relationships

This topic contains 15 replies, has 3 voices, and was last updated by  Noman 1 week, 1 day ago. The support staff assigned to this topic is Noman.

Viewing 15 posts - 1 through 15 (of 16 total)
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
http://wp-types.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:
http://test.biggreenegg.co.za/view-shops-with-eggsperts-2/

#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
Forum moderator

Supporter languages: English (English )

Supporter 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
Forum moderator

Supporter languages: English (English )

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

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:
http://test.biggreenegg.co.za/view-shops-with-eggsperts-2/

#556460

Noman
Forum moderator

Supporter languages: English (English )

Supporter 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
Forum moderator

Supporter languages: English (English )

Supporter 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
Forum moderator

Supporter languages: English (English )

Supporter 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: http://test.biggreenegg.co.za/view-shops-with-eggsperts-2/

Thank you

#557366

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

#557391

Noman
Forum moderator

Supporter languages: English (English )

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

Hello williamm-5 ,

The code and steps were same as mentioned here:
https://wp-types.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
Forum moderator

Supporter languages: English (English )

Supporter 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: http://www.biggreenegg.co.za/find-an-eggspert/

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

Viewing 15 posts - 1 through 15 (of 16 total)

You are not logged in. You can view support threads, but not post.
If you are already logged in, please refresh your browser.

Known issues and their solutions

We document known issues and their solutions in Toolset erratas. Save time by checking there first and seeing if what you are facing is a known issue. This list is kept short. We do our best to resolve all known issues with every release of Toolset plugins.

Support Policy

Toolset staff will help you use the Toolset plugins correctly, provide any information you need, help troubleshoot problems and advice on best-practices.

Please make sure to issue only one topic per each support ticket.

For more information, read our full Customer Support Policy.

Need custom programming work for your site? Check out our certified partners.