Skip Navigation

[Resolved] Conditional output involving count of images in a repeating field not working

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

Problem:

Count custom repeating field number with custom codes.

Solution:

It needs custom codes, for example:

https://toolset.com/forums/topic/conditional-output-involving-count-of-images-in-a-repeating-field-not-working/#post-1315173

Relevant Documentation:

This support ticket is created 5 years, 4 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/Hong_Kong (GMT+08:00)

This topic contains 7 replies, has 2 voices.

Last updated by ericE-4 5 years, 4 months ago.

Assisted by: Luo Yang.

Author
Posts
#1314159

I am trying to: evaluate the output from a custom shortcode that counts the number of images in a repeating field. I used the same code as from here (https://toolset.com/forums/topic/show-count-of-repeating-field-entries/), but here it is again:

// shortcode to count number of repeating field instances
add_shortcode( 'counter', 'counter_func' );
function counter_func($atts) {
    return sizeof(get_post_meta( get_the_ID(), 'wpcf-' . $atts['field'], false ));
}

The shortcode works properly when I use it to display the count like this:

[counter field='meals-photos']

But when I use it in a conditional statement in a content template, it breaks and nothing gets output:

[wpv-conditional if="( '[counter field='meals-photos']' = '0' )"] DO THIS [/wpv-conditional]

Is there something wrong with the syntax of the conditional statement? Unfortunately, I can't use the Conditional Output Shortcode wizard, as it doesn't allow usage of the 'field=' parameter.

#1314215

Hello,

Since it is a shortcode created with custom PHP codes, please try this:
Dashboard-> Toolset-> Settings-> Front-end Content, in section "Functions inside conditional evaluations", add the shortcode name: counter

And test again, more help:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

#1314219

Yes, I have already done that. That allows me to see the shortcode in the Conditional Output Shortcode wizard. But as I said, it doesn't allow use of the 'field=' parameter (so is kinda useless).

#1314229

You can use double quotes insider [count] shortcode, for example:

[wpv-conditional if="( '[counter field="meals-photos"]' eq '0' )"] DO THIS [/wpv-conditional] 

I have tested it in my localhost, it works fine.

#1314823
Screenshot 2019-08-12 11.32.18.png

It doesn't work for me...nothing gets output still. Also, the template editor thinks that the syntax with the double quotes inside the shortcode (now renamed to "count-repeater") is invalid. See the red colour.

#1315141

Since it is a custom PHP codes problem, please provide a test site with the same problem, also point out the problem page URL and where I can edit your PHP codes, I need a live website to test and debug, thanks

#1315149
#1315173

Thanks for the details, I can log into your website and see the problem.

The problem is the custom shortcode [count-repeater] does not work in your website at all, when there isn't any item in field "meals-photos", it outputs value 1, so it conducts the problem.

I have change the PHP codes as below:

add_shortcode( 'count-repeater', 'counter_func' );
function counter_func($atts) {
    $filed = 'wpcf-' . $atts['field'];
 	$values = get_post_meta( get_the_ID(), 'wpcf-' . $atts['field'], false );
    $res = 0;
    if(is_array($values) && !empty($values[0])){
        $res = sizeof($values);
    }
    return $res;
}

Please test again, check if it is fixed, thanks

#1315195

I used the same code as from the solution in this case (https://toolset.com/forums/topic/show-count-of-repeating-field-entries/), which I guess also did not work. Hopefully the person who opened that support ticket eventually figured out that the code given to him was faulty.

For the benefit of others who may need this same functionality (this really should be something built in), one fo the lines in the code you provided was redundant so I remove it. The final code to count the number of items in a repeating field is:

// shortcode to count number of repeating field instances
add_shortcode( 'count-repeater', 'counter_func' );
function counter_func($atts) {
 	$values = get_post_meta( get_the_ID(), 'wpcf-' . $atts['field'], false );
    $res = 0;
    if(is_array($values) && !empty($values[0])){
        $res = sizeof($values);
    }
    return $res;
}

And also the editor validation when creating a content template is not accurate, so even if it says the code has errors, it doesn't necessarily mean that's true.

My issue is resolved now. Thank you!