Skip Navigation

[Resolved] How to add a counter to the loop in views?

This support ticket is created 6 years, 3 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 7 replies, has 2 voices.

Last updated by Shane 6 years, 3 months ago.

Assisted by: Shane.

Author
Posts
#1071361

Hello,

I would like to add a counter to my views output, so I can get the following result:

1. Item 1
2. Item 2
3. Item 3
.............
n. Item n

How I get the numbers 1,2,3 to be dynamically created. I have tried with an ordered list, but pagination breaks it and on the next page the items starting to be counted again from 1. It's actually a very basic feature I have used before using another CMS Drupal and its built-in module Views, but I can't find it in your Types and Views plugins.

I would appreciate any help.

Best regards,
Damian

#1071411

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Damian,

Thank you for contacting our support forum.

You can achieve this by using some custom code.

/**
 * Add loop-iteration shortcode
 * 
 * Attributes
 * 'n' (optional) : test for nth iteration
 */
add_shortcode( 'loop-iteration', function( $atts ){
 
    global $loop_n;
 
    if ( !isset( $loop_n ) ) {
        $loop_n = 0;
        return $loop_n;
    }
 
    $loop_n++;
 
    if ( !isset( $atts['n'] ) ) {
        // no nth parameter, just return the current index
        return $loop_n;
    }
    else {
        $remainder = ( $loop_n + 1 ) % $atts['n'];
        return ( $remainder == 0 );
    }
 
});

Add the above to your functions.php file and then you can use the shortcode [loop-iteration] to get the number in the current loop.

Thanks,
Shane

#1071462

Hi Shane,

thanks for the code, but it works only on the first page. As I mentioned I'm using pagination and on the second page, it's starting to count again from 0.

Best,
Damian

#1071481

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Damian,

I believe that its because the state is lost because of the refresh of data and the new query.

I'm actually not sure how this one can be resolved but an advise is that you can possibly store the last value .

This one is actually out of the scope that I can provide support for, however what you can do is to contact one of our registered contractors for more advanced assistance.

https://toolset.com/contractors/

Thanks,
Shane

#1074914

Hi Shane,

thank you for your support. I understand you have tried to help me, but it looks like the plugin doesn't have the basic feature which is 'view result counter'.

I'm very satisfied with your support but very disappointed with the plugin Views. I would understand if it was a free plugin without advanced features, but I have paid for this plugin and now I have to spend additional money to hire a contractor for the must-have feature of every table to display actual position of the view result.

Do you plan to add this option in future?

Best regards,
Damian

#1076385

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Damian,

What I can do is to request this for you.

However you can try the infinite scroll feature which should still paginate as well as giving you the list count that you want?

Please let me know if this helps.
Thanks,
Shane

#1080255

Hi Shane,

please request it for me.

Thank you for the idea, the infinite scroll it's a nice workaround.

Best regards,
Damian

#1081443

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Damian,

Great, could you create a feature request ticket asking for this so that it can be assigned to the appropriate supporter for this ?

Thanks,
Shane