Skip Navigation

[Resolved] how to generate incremental value in view

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

Problem: I would like to generate an incremental value in a view.

Solution: You can use these custom shortcodes in a non-paginated, non-AJAX View:

global $tslooptotal;
$tslooptotal = 0;
 
function add_total_shortcode($atts, $content = '') {
  global $tslooptotal;
  $tslooptotal += $content;
}
add_shortcode('add-to-total', 'add_total_shortcode');
 
function show_total_shortcode() {
  global $tslooptotal;
  $totalNew = $tslooptotal;
  return $totalNew;
}
add_shortcode('show-total', 'show_total_shortcode');

Then in the Loop:

<wpv-loop>
Loop number: [show-total]<br />
Total again: [show-total]
[add-to-total]1[/add-to-total]
</wpv-loop>
This support ticket is created 6 years, 4 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.

Our next available supporter will start replying to tickets in about 0.43 hours from now. Thank you for your understanding.

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 6 replies, has 2 voices.

Last updated by teganK 6 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#952848

I am stuck with an issue
i have tabs in a loop
but as u know tabs need unique id to work
as view is not allowing passing php code.
I wrote an incremental function and created a shortcode for it
i then tried placing that shortcode in view.
it start working for some locations but for other i got stuck
e.g this part dont work
<button class="tablinksgallery" onclick="openTabGallery(event,'InteriorInspiration[incrementor]')">Interior Inspiration [incrementor]</button>

this part works
<div id="InteriorInspiration[incrementor]" class="tabcontentgallery" >[rev_slider alias="home-design-variation-II"]</div>

can some 1 please guide me how to make the above work
or is there any way i can write php in view or if i can set a variable in loop.
i want unique name to the tabs and that is only possible if i put an incremental value
Thanks in advance.

#953151

i want unique name to the tabs and that is only possible if i put an incremental value
Post IDs are unique, and you can access the current post ID or term ID in the Loop to create a unique name. Something like "interior-insp-[wpv-post-id]" without a custom incrementor.

If you want to write a custom incrementor shortcode, you'll have to use a PHP global and a combination of shortcodes - one to increment the value, and another to show the value. Otherwise each time you place the incrementor shortcode, the number will increment. That means you can't place it twice in the same loop.

#953154

the difficulty i am facing after placing the short code is
ever time i call that shortcode it generates a new value.
where as i want 1 value for each loop

will you please be kind enough to guide how can i keep the same value for each item using shortcode in loop?

thanks in advance,

#953209

I have an idea but it only works for non-AJAX, non-paginated, non-nested Views in very simple cases. Add this code to your functions.php file:

global $tslooptotal;
$tslooptotal = 0;

function add_total_shortcode($atts, $content = '') {
  global $tslooptotal;
  $tslooptotal += $content;
}
add_shortcode('add-to-total', 'add_total_shortcode');

function show_total_shortcode() {
  global $tslooptotal;
  $totalNew = $tslooptotal;
  return $totalNew;
}
add_shortcode('show-total', 'show_total_shortcode');
<wpv-loop>
Loop number: [show-total]<br />
Total again: [show-total]
[add-to-total]1[/add-to-total]
</wpv-loop>
#953246

thanks

#953650

You are welcome, let me know if you have problems with the code.

#958247

thanks christian cox solved myissue.

but this plugin need a lot of basic option which make life more difficult and depend too much on support if basic options are provided these basic issues will not be requiring support