Tell us what you are trying to do? I would like to be able to set a conditional statement that adds a certain css class if the number of repeating fields is more than 3. For example, I have a repeating field for gallery images, and if there are more than 3 images added, I'd like to add the class "plus" to the parent/enclosing div.
Is there any documentation that you are following? For this use, I haven't found anything that works yet.
Is there a similar example that we can see? That would be nice.
What is the link to your site? It's a local dev site right now.
You cannot officially count the iterations of a Repeating Field (you can address it in a foreach loop or similar, but not just count and decide upon the amount of instances what to do).
But, you can apply this custom code to count repeating instances of a field:
1. Register a new ShortCode like:
add_shortcode( 'count-repeats', 'count_repeat_func' );
function count_repeat_func($atts) {
return sizeof(get_post_meta( $atts['post-id'], 'wpcf-' . $atts['field'], false ));
}
2. Then you can use this ShortCode.
You should wrap this ShortCode in an HTML Conditional to check if the Field does exist, as other wise unexpected results can be produced:
[count-repeats post-id="6184" field="url"]
Since this will return the site of (amount) repeating instances, you can then use this value as a Conditional to check upon.
Hi Beda, thanks for the help. I did see this solution in my hunting, but it doesn't quite suit my needs as it is. Would it be possible to modify it so that the output is the number, spelled out instead of the numerical value?
e.g. "one" instead of "1", "two" instead of "2" etc
I'm guessing not, but it never hurts to ask!
Thanks,
I would not say it's impossible, but I wonder what the idea is behind it.
You want to use this as a conditional later, correct?
In this case it's definitely better to use a number, which can increase and stay small, instead of words, which increase and do not stay small.
I mean, it's safer, easier and nicer to work with numbers in my opinion.
But, if you want to output counts in words, you can use different approaches:
https://stackoverflow.com/questions/11500088/php-express-number-in-words
I would not recommend it thou for a conditional data pool.
Here's how I would use it. I could embed the "count" shortcode into the class area of the enclosing div. I could then apply the same css to classes "one", "two" and "three", and apply different css to everything else. It would enable me to create the same outcome because I just need to modify the gallery layout if there are more than 3 images added. Does that make sense?
Thanks for the help, Beda.