Skip Navigation

[Resolved] I get PHP warning for wpv_conditional_post_has_content() function (PHP 7.3)

This support ticket is created 5 years, 3 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

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

This topic contains 2 replies, has 2 voices.

Last updated by cassianS 5 years, 3 months ago.

Assisted by: Waqar.

Author
Posts
#1188402

Hi, I am using this function to check for an empty container of a post body:

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

I got from this article: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/

When I updated my php version to 7.3. I started to get a php error:
"Warning: count(): Parameter must be an array or an object that implements Countable in ... "

I need your help to fix this function so it would work for PHP 7.3

Link to a page where the issue can be seen:
hidden link

#1188938

Waqar
Supporter

Languages: English (English )

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

Hi there,

Thank you for contacting us and I'll be happy to assist.

That code is shown on the guide as an example and for PHP 7.3+, you can update your code to:


function wpv_conditional_post_has_content($type, $object) {
    $return = 0;
    if ( $type == 'posts' ) {
        if ( empty( $object->post_content ) ) {
        	// return 0 when post's content is empty
            $return = 0;
        } else {
        	// return 1 when post's content is not empty
            $return = 1;
        }
    }
    return $return;
}

After that, you'll be able to use this function in conditional blocks as:


[wpv-conditional if="( wpv_conditional_post_has_content() eq '0' )"]
This post has no content
[/wpv-conditional]

[wpv-conditional if="( wpv_conditional_post_has_content() eq '1' )"]
This post has some content
[/wpv-conditional]

I hope this helps and please let me know how this goes.

regards,
Waqar

#1190571

My issue is resolved now. Thank you!

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