[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.
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.
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.
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.