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.
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/
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).
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.
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.
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
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
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!