WordPress do_shortcode first iteration

Figured out both issues. You can keep a count and find out when you are on the first iteration in the same go.

static $count;
if(!$count) {
    $count = 1;
    $state=" active";
} else {
    $count++;
    $state="";
}

From the PHP Manual. “A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.”

This code sets a static variable within the recursive function, in this case a function called by do_shortcode(). This lets you keep a count going so if you wanted to have id’s like #tab-1, #tab-2 you could easily do so. With this code you can also tell when the first iteration happens. I set a $state variable and give it an active css class. Since I’m appending it to a class attribute already I’ve only got active, but you could also pass the whole thing if you only had one class like class=”active” for example.