Skip Navigation

[Resolved] conditional test for content number of words

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 3 replies, has 2 voices.

Last updated by Minesh 1 year, 9 months ago.

Assisted by: Minesh.

Author
Posts
#2559013

Tell us what you are trying to do?

conditional test for number of words in the custom post content with shortcode and then conditionally print the post content or an alternate message. I don't test for custom post type in my function but it is in a view which only queries my article custom post.

My shortcode:

function wpbeginner_get_wordcount($post_id) {
     //Get the post, remove any unnecessary tags and then perform the word count// 
     $wpbeginner_wordcount = str_word_count( strip_tags( strip_shortcodes(get_post_field( 'post_content', $post_id )) ) );
      return $wpbeginner_wordcount;
}
// short code to return count of characters in article content [wpbeginner_get_wordcount]
add_shortcode('wpbeginner_get_wordcount', 'wpbeginner_get_wordcount');

Views code:

       Count= [wpbeginner_get_wordcount]<br>    /*** note this works and outputs correct wordcount

        [wpv-conditional if="( wpbeginner_get_wordcount() gte '6')"]
         [wpv-post-body view_template="None"]
        [/wpv-conditional]
        [wpv-conditional if="( wpbeginner_get_wordcount() eq '0')"]
          No abstract available.
        [/wpv-conditional]

Output: example
Count = 244
No abstract available.

Conclusion:
The conditional test is always eq '0'? This test is always true for some reason even though the function as shown by the shortcode is clearly not

What is the link to your site?
hidden link

Thanks for looking at this.

#2559291

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can you please try to use the following shortcode:

function wpbeginner_get_wordcount($post_id) {
    global $post;
   $post_id = $post->ID;

     //Get the post, remove any unnecessary tags and then perform the word count// 
     $wpbeginner_wordcount = str_word_count( strip_tags( strip_shortcodes(get_post_field( 'post_content', $post_id )) ) );
      return $wpbeginner_wordcount;
}
// short code to return count of characters in article content [wpbeginner_get_wordcount]
add_shortcode('wpbeginner_get_wordcount', 'wpbeginner_get_wordcount');

Where:
- As you may noticed with the above shortcode we defined the global $post to access the current post ID in the loop.

Then, can you please make sure that you registered the shortcode "wpbeginner_get_wordcount" at:
=> Toolset->Settings and click on the Front-end Content tab. Look for the section called "Third-party shortcode arguments" and register your shortcode to this section.

#2559565

Thanks, but it made no difference. Same result. I'm wondering if the problem is in the conditional statement in the toolset archive because the shortcode for the function used in my toolset archive loop editor:

Count= [wpbeginner_get_wordcount]

is giving the correct count of words but it is not being used correctly in the conditional. (even though I have registered it as you said in "Third-party shortcode arguments" and as well in "Functions inside conditional evaluations"

Here is the conditional code in the loop editor:

Count= [wpbeginner_get_wordcount]<br>
[wpv-conditional if="( wpbeginner_get_wordcount() ne '0' )"]
        [wpv-post-body view_template="None"]
        [/wpv-conditional]
        [wpv-conditional if="( wpbeginner_get_wordcount() eq '0' )"]
          No abstract available but the full text pdf may be downloaded at the title link below.
        [/wpv-conditional]

Result:
The test always fails and outputs my zero-case statement. Yet the shortcode count before the conditional shows the correct count?

SOLUTION:
After searching the forum I found this post:
https://toolset.com/forums/topic/conditional-syntax/
which offered a clue to the problem in the conditional. I replaced the above code in the loop editor with:

        [wpv-conditional if="( '[wpbeginner_get_wordcount]' ne '0' ) " ]
        [wpv-post-body view_template="None"]
        [/wpv-conditional]
        [wpv-conditional if="( '[wpbeginner_get_wordcount]' eq '0' ) " ]
          No abstract available but the full text pdf may be downloaded at the title link below.
        [/wpv-conditional]

And this works correctly.

Note: the use of the shortcode surrounded by single quotes. This works and the function method (top code) does not.

Many thanks for your help, Minesh, particularly your forum post I found.

#2559899

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Glad to know that you found the related information you required with the forum ticket where I shared the solution.

You are welcome to mark resolve this ticket.