Skip Navigation

[Résolu] Count repeating items in loop

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
How can I count how many items of a reapating fields are present in my Views' loop?

Solution:
https://toolset.com/forums/topic/setting-conditional-output-based-on-the-number-of-repeating-field-items/#post-616727

This support ticket is created Il y a 6 années et 9 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Ce sujet contient 4 réponses, a 2 voix.

Dernière mise à jour par joshM-4 Il y a 6 années et 9 mois.

Assisté par: Beda.

Auteur
Publications
#616616

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.

#616727

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.

#616897

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,

#616975

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.

#617070

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.