RTO-Websites / dynamic-conditions

A simple plugin to show/hide elementor-widgets based on dynamic tags
52 stars 17 forks source link

Show element only if current post has children #39

Closed globdug closed 3 years ago

globdug commented 4 years ago

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?

crazypsycho commented 4 years ago

Hello @globdug

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

grafik

globdug commented 4 years ago

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?

crazypsycho commented 4 years ago

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

globdug commented 4 years ago

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