I've created a recursive shortcode that looks like this.

[tab state="active"]Home[/tab]

[tabpane]Content 1[/tabpane]
[tabpane]Content 2[/tabpane]
[tabpane]Content 3[/tabpane]

It is working, but I would like to remove the state="active" and automatically add that to the first tab, but I'm not sure how to tell which iteration my do_shortcode is on?

Update: What would be even better is if I could keep a count while iterating through the do_shortcode so I could add attributes like id="tab1", id="tab2".

1 Answer 1


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 {
    $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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.