Skip Navigation

[Resolved] Specifying which view an Incrementor Shortcode looks at

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

Problem:
Client is using a custom shortcode to display an incrementing value for each loop iteration, but this breaks when using it in multiple Views on the same page. How can the code be updated to work across Views?

Solution:
This is custom code, which is not supported, though a suggested update to the code would be as follows:

//Loop Counter
add_shortcode('incrementor', 'incrementor');
function incrementor( $atts = [] ) {
 
    $atts = shortcode_atts( [ 'index' => 'i'], $atts );
 
    static $indices = array();
 
    if ( !isset( $indices[ $atts['index'] ] ) ) {
        $indices[ $atts['index'] ] = 0;
    }
 
    $indices[ $atts['index'] ] ++;
 
    return $indices[ $atts['index'] ];
}

// in each View provide a different index, like so:
[incrementor index="a"]
This support ticket is created 6 years, 9 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
- 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)

This topic contains 3 replies, has 2 voices.

Last updated by Nigel 6 years, 8 months ago.

Assisted by: Nigel.

Author
Posts
#620724

As per an earlier support thread, I'm using the code below to count loop items...

//Loop Counter
add_shortcode('incrementor', 'incrementor');
function incrementor() {
static $i = 1;
return $i ++;
}

The problem is that if I have two or more view loops on a single page, the incrementor counts the loop items in them continually.

For example:
2 Views placed one after the other on the same page...
View 1 has 3 items
View 2 has 5 items

For View 1, the incrementor returns 1, 2, 3 (which is correct)
For View 2, the incrementor returns 4, 5, 6, 7, 8 (which is incorrect)

I need the Incrementor to begin counting at 1 for each individual View I use it within (In the above example, View 1 would then return 1, 2, 3, 4, 5).

I imagine the way to achieve this is to get the incrementor to look for a specific Div - would it be possible to do this with an attribute in the shortcode?

For Example, if the View is placed within a div with class="count-within-div", the shortcode could look something like [incrementor attr="count-within-div"]

If not the above, is there another way of achieving this?

Thanks in Advance!

#620813

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

The problem with re-using the same shortcode is that the same counter index ($i) is being used each time, and you need different counters for each time you use the shortcode on the same page load.

I modified your code so that the shortcode takes an attribute 'index' which is the variable name to use. If you are only using this shortcode once you can omit it and it will default to 'i'.

Here's the code:

//Loop Counter
add_shortcode('incrementor', 'incrementor');
function incrementor( $atts = [] ) {

	$atts = shortcode_atts( [ 'index' => 'i'], $atts );

	static $indices = array();

	if ( !isset( $indices[ $atts['index'] ] ) ) {
		$indices[ $atts['index'] ] = 0;
	}

	$indices[ $atts['index'] ] ++;

	return $indices[ $atts['index'] ];
}

Use it like so:

[incrementor index="a"]
#623001

Hi Nigel,

I see this working now, though I notice it resets when a next page is loaded in pagination.

I have an archive page listing 114 items that load 24 items at a time with infinite scrolling. The problem is each batch of 24 starts at 1 so I cannot set a rule based on the incrementor equalling the found count.

Is there any way of resolving this?

#623008

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The global variables used in the shortcode are being reset by the pagination, and that cannot be helped.

You would need a more complex solution to persist the tally going across pagination updates.

I wouldn't know what to suggest without first doing some testing to confirm what works and what doesn't, and that is not really within the scope of the support we offer.

I suggest you contact one of the Toolset contractors for help, although any experienced WordPress developer may be able to advise you.