RTO-Websites/dynamic-conditions

Show element only if current post has children

globdug opened this issue · 4 comments

Hello,
is there a way to show an element only if current post has children?

I would like to show a menu of child pages only if current post has children.

I created a little shortcode that load child pages

function list_child_pages(){
	global $post;
	$childpages = wp_list_pages([
		'post_type' => $post->post_type,
		'echo' => false,
		'child_of' => $post->ID,
	]);
	$list = '<ul>' . $childpages . '</ul>';
	return $list;
}
add_shortcode('child_pages', 'list_child_pages' );

I would like to manage the condition with Dynamic Conditions plugin, not via php. Is it possible?

Hello @globdug

you could select Shortcode as dynamic-tag and enter your shortcode, than check if it contains <li.

grafik

Thank you!

While I was waiting for a reply, I solved in a similar way. :)

I have changed the return of function in this way:

if( $childpages=='' ){
	return '';
}else{
	return '<ul>' . $childpages . '</ul>';
}

And in DC I set the condition to "Is not empty".

There could be some issues if I use this approach in your opinion or is better yours?

I don't think it makes a big difference.
It's more a matter of taste how to do it.

Ok, thank you very much for your help and time!