Pro 4.0.8: Bug in looper index. Referencing Last ends up referencing the penultimate

Hi there,
This is a bug that I have come across when trying to create my own custom breadcrumbs within Divs etc for a Post Layout. This pulls in taxonomies via a Current Post Terms and then lays them out with a separate Text element for the delimiter (a comma) which I want to hide from the last item, so that it only puts a comma between terms, and not at the end as well.

After extensive testing, I have found that the Index seems to be showing incorrectly when using the Current Post Terms looper provider. I have set up an example of this to show the difference between using Current Post Terms, which has an incorrect Index, and Recent Posts, which shows the correct Index:

The top list, “Climate Change, Health & WellbeingSustainability,” is using the Current Post Terms provider, and you can see that the index is beginning at 2 rather than 1, which is leading to the comma being hidden on the second term, rather than the last.

The bottom list is using the Recent Posts provider, and the index begins at 1 as expected, and the comma disappears for the last item in the list as expected.

I have found that by adding the following lines to the Cornerstone_Looper_Provider_Terms class I have been able to fix the problem, and the Index is correctly numbered (and the subsequent Conditional on the comma text element works correctly too)

public function get_index() {
	return $this->size - count( $this->items ) - 1;
}

The extra - 1 could be added to the Cornerstone_Looper_Provider_Array class, but I wasn’t sure if that would have further-reaching effects that would be unwanted?

Not sure if the fix is as simple as that, but hoped this might help!

Thanks @arthurodb! You have achieved full looper mastery with this one :joy: I’m really grateful that you took some time to track down what’s going on in the code - that’s a huge head start for me. I’ll take a closer look and get something worked out and also has consistency across all looper types.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.

Hi @arthurodb,

Just wanted to let you know I got this sorted out for the next point release. Again, a huge thank you! Your solution was actually what I ended up going with. I did some extra checking to make sure there weren’t other side effects. After reviewing it all, I’m certain the get_index function should return a zero based index, and the dynamic content callback adds 1 to it so the output is more human friendly. That is keeping the behavior consistent with all loopers.

1 Like