Skip Navigation

[Résolu] Repeat a content template X times based on CPT value.

This support ticket is created Il y a 5 années et 5 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

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

Dernière mise à jour par Nigel Il y a 5 années et 5 mois.

Assisté par: Nigel.

Auteur
Publications
#1267291

Hi,

I’m looking to achieve the ability to repeat a code snippet or a content template based on the value the user puts in a custom field.

Perhaps there are several ways to achieve this.. what is the most straight forward?

Example:
Custom Field Value = 3

Output

<ul>
  <li class="instance-0001">A copy of my content</li>
  <li class="instance-0002">A copy of my content</li>
  <li class="instance-0003">A copy of my content</li>
</ul>

Thanks in advance!

P.S. If you would like to read more about why I need such functionality - please continue reading...

I am trying to solve a “big data” issue by adressing it using front-end logic.
So instead of 1000 separate posts from another post type, I’d like to create a “post group type” that will hold a variable inside a custom field. Then using a content template and that value, I will output as many copies of that template on the front end as the custom field value says. Further more - I want to input user generated data and tie it to a specific copy. So if the user generates new data for copy #734. I will execute a search and replace function on the front-end that will replace the generic content that all copies have with his data(or append instead of replace). This way I will have two custom post types that can hold the data: - a single entry in the “post group cpt” will hold the data for amount of virtual generic copies(that come from a single content template); a number of unique user generated entries holding data for a specific virtual entry based on the unique number identifier we’re giving to each copy’s class or id. Within the content template I will have a form that will collect the user generated data and also collect the unique number identifier from it’s parent id or class and store it inside a custom field that will later serve as an anchor for the search and replace/append function.

#1267699

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

Toolset offers two ways to iterate over content.

Views will iterate over posts (or users or taxonomy terms).

The wpv-for-each shortcode (https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-for-each) will iterate over multiple instances of a custom field.

Neither of these is applicable to your use case, and so you will need to create a custom solution.

I imagine you might register a custom shortcode which gets the value of the custom field which you then use to set up a loop that generates the markup for each iteration using the render_view_template function (https://toolset.com/documentation/programmer-reference/views-api/#render_view_template) to return the output of the content template. This will get more complex where you need to generate different output for certain iterations, but the logic will need to be contained within the same shortcode.

See https://developer.wordpress.org/plugins/shortcodes/ about registering custom shortcodes.

I don't have much else to add, writing custom code for such a project falls outside the scope of our support.

#1277833

I’ve actually made some great progress here by running javascript from within the loop body (and then since it’s inside the loop, the loop loops my javascript does the magic, without having to write special loop function in javascript.

I’d like to keep the ticket open where I will be sharing my solution soon.

#1279479

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

Sure, please share what you came up with when you have it working.