Primus202
Primus202

Reputation: 646

Wordpress Pods: Index in Template

Is it possible to get a pod items index in the template? For instance, if I have a pods variable that has ten entries in it, could I make a template that renders each entry with its place in the list or, even better yet, if it's first/last as such:

Rendering item {@index} out of {@total} called {@title}

Note: I mean the index in the scope of the group of items being rendered. Not the posts ID or something else. If this doesn't exist it would be a great feature to have!

Upvotes: 0

Views: 1421

Answers (3)

Kurt Koller
Kurt Koller

Reputation: 448

PHP is deprecated in Pods templates. Now you should use Pods Frontier plugin or put PHP code in WP templates.

Upvotes: 0

Dau Thanh Hai
Dau Thanh Hai

Reputation: 1212

I have the same requirement. My scenario is that I need to detect first element so that I can display it in a slideshow. Inside template, I declare a global variable:

 <?php global $counter; 
    if ($counter == 0) {
        $class='active'; 
        $counter++;
    } else {
        $class='';
    }?>

Then I echo variable $class to where i want. By this way, I cannot know totally how many row but It sold my problem.

Upvotes: 0

Scott Kingsley Clark
Scott Kingsley Clark

Reputation: 987

It's possible with PHP:

Total in this list: <?php echo $obj->total(); ?>

Or you can get the total (across all pages, if using pagination or limiting):

Total Found: <?php echo $obj->total_found(); ?>

Or you can get the current position in the loop (new in Pods 2.3):

Current Position: <?php echo $obj->position(); ?>

Or you can even do an nth check (CSS nerds know what I'm talking about in regards to how nth-child works):

<?php
    if ( $obj->nth( 'even' ) )
        echo 'even row';

    if ( $obj->nth( 'odd' ) )
        echo 'odd row';

    if ( $obj->nth( '1n+3' ) )
        echo 'you get the picture';

    if ( $obj->nth( '3n+0' ) )
        echo 'you get the picture';
?>

For info about nth, it just takes the same input as nth-child does in CSS: http://www.w3schools.com/cssref/sel_nth-child.asp

If you'd like these available as magic tags, please submit a feature request at http://pods.io/submit/

Upvotes: 1

Related Questions