Skip Navigation

[Resolved] Conditional Output if the post content is filled

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

Problem: I would like to use conditional HTML to test whether or not the post content is empty.

Solution: You can use a custom function to test if the body contents are empty:

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;
}

Go to Toolset > Settings > Frontend Content > Functions inside conditional evaluations, and enter wpv_conditional_post_has_content to register this function for use in conditionals.

Then you can use the function in a conditional like this:

[wpv-conditional if="( wpv_conditional_post_has_content() eq '1' )"]
<a href="#person-gallery"><button type="button" class="btn btn-primary">[wpml-string context="Person Gallery Anchor" name="Person Gallery Anchor"]الصور / الأيقونات المتاحة[/wpml-string]</button></a>
[/wpv-conditional]

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

This support ticket is created 6 years, 5 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 AtefR7377 6 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#907875

Hi,
I have a CPT called "Persons". I created a layout for it.
In the layout, I want to display a ootstrap button if the post content is not empty.

I tried the following code, but nothing showed:

[wpv-conditional if="( $(wpv-post-body) ne '' )"]
<a href="#person-gallery"><button type="button" class="btn btn-primary">[wpml-string context="Person Gallery Anchor" name="Person Gallery Anchor"]الصور / الأيقونات المتاحة[/wpml-string]</button></a>
[/wpv-conditional]

So my question is:
Is there a way to say: if the post content field is not empty, show the button?

thanks

#908411

Hi, you can use a custom function to test if the body contents are empty:

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;
}

Go to Toolset > Settings > Frontend Content > Functions inside conditional evaluations, and enter wpv_conditional_post_has_content to register this function for use in conditionals.

Then you can use the function in a conditional like this:

[wpv-conditional if="( wpv_conditional_post_has_content() eq '1' )"]
<a href="#person-gallery"><button type="button" class="btn btn-primary">[wpml-string context="Person Gallery Anchor" name="Person Gallery Anchor"]الصور / الأيقونات المتاحة[/wpml-string]</button></a>
[/wpv-conditional]
#908624

Thanks a lot Christian for your awesome support every single time. very much appreciated.