Skip Navigation

[Resolved] Generate a random ID for each loop

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to use the same View in multiple places on the same page, but I want to avoid unique IDs.

Solution: Use wpv-attribute to help generate that unique ID. For example, let's say you use the same View in two places on the same page. Add the placement attribute, and set some unique value for each View:

[wpv-view name="Your View Name" placement="abc"]
[wpv-view name="Your View Name" placement="def"]

Then inside the loop, you can access the placement attribute to make the IDs unique.

<div class="collapse" id="collapse-[wpv-attribute name='placement']-[wpv-post-id]">

Results:

<div class="collapse" id="collapse-abc-123">
...
<div class="collapse" id="collapse-def-123">

Relevant Documentation:
https://toolset.com/documentation/user-guides/passing-arguments-to-views
https://toolset.com/documentation/user-guides/views-shortcodes/#vf-309292

This support ticket is created 5 years, 5 months ago. 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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by deanL 5 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#1139449

Hi, I can understand if this falls beyond your usual scope of support. But I'm losing my hair with this one here. As your "Toolset for life" customer, I hope you are able to help me with this, even if you just point me to certain direction.

I'm using the following code to output content from CPT (created using Toolset of course!):

<div class="row">
<div class="col-sm-3 person-photo">
[wpv-post-featured-image size="full"]
</div>
<div class="col-sm-8 person-details">
<h3>[wpv-post-title]</h3>
<div>[types field="title"][/types]</div>
[wpv-conditional if="( ($(wpcf-email) ne '' OR $(wpcf-contact-number) ne '') )"]
<a class="btn btn-primary" role="button" data-toggle="collapse" href="#collapse[wpv-post-id]"><span class="glyphicon glyphicon-chevron-down" aria-hidden="true"></span></a>
<div class="collapse" id="collapse[wpv-post-id]">
<div class="well">
<!-- additional data goes here -->
</div>
</div>
[/wpv-conditional]
</div>
</div>

As you can see, I'm using Bootstrap Collapse to display additional content when it's available, triggered with A tag. In order to make sure the trigger works with only relevant content, I use [wpv-post-id] to uniquely identify each loop.

This worked very well until my client requested me to output some of the content more than one instance within the same page. With this additional requirement, now I have to find way to make sure each loop has a unique ID, which I have no idea how to achieve it!

I'm hoping to know if Toolset has a shortcode for generating unique code within a loop? That way I can append the code to HREF and corresponding div's ID so make each loop unique.

Am I making sense?

#1139835

There's no built-in unique identifier shortcode, but you could use wpv-attribute to help generate that unique ID. For example, let's say you use the same View in two places on the same page. Add the placement attribute, and set some unique value for each View:

[wpv-view name="Your View Name" placement="abc"]
[wpv-view name="Your View Name" placement="def"]

Then inside the loop, you can access the placement attribute to make the IDs unique.

<div class="collapse" id="collapse-[wpv-attribute name='placement']-[wpv-post-id]">

Results:

<div class="collapse" id="collapse-abc-123">
...
<div class="collapse" id="collapse-def-123">

More about passing shortcodes into Views:
https://toolset.com/documentation/user-guides/passing-arguments-to-views
https://toolset.com/documentation/user-guides/views-shortcodes/#vf-309292

#1140063

Christian, you are my superstar! The discovery of [wpv-attribute] saves the day for me! In my view I'm already using a taxonomy filter, so I just added the attribute to the HTML code and voila! Problem solved!

Backstory: From the code you could see it's somewhat about displaying employees info. What happened was, some employees are under more than one department (taxonomy), it's rather silly to show duplicated info within the same page, but client wants it that way. I almost lost hope since the site was almost ready to go live and I got hit by such challenge!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.