Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Possible Duplicate:
how do I get_sidebar into a varaible?

I found how do I get_sidebar into a varaible? but the goal was different than mine, so the answers don't apply.

I want to get a sidebar and save it into a variable so that I can create an if/elseif statement that will print the correct sidebar depending on my conditional criteria. For example, I currently have something like this...

if ( is_page(1) ) {
    $sidebar = get_sidebar('wine');
} elseif ( is_page(2) ) {
    $sidebar = get_sidebar('cheese');
} elseif ( is_page(3) ) {
    $sidebar = get_sidebar('whatever');
} else {
    $sidebar = get_sidebar('primary');
}

I'm not actually using is_page in my conditional statements and I am setting more than just the $sidebar variable, but you get the idea. Further down in the code I want to do this...

echo $sidebar;

The sidebars I am working with are not all dynamic, but some are. So, is there a function besides get_sidebar() that I can use?


UPDATE = ANSWER

Oh, why did I hit enter? The answer left by goldenapples on the question I linked to (above) did do exactly what I wanted. When I first read it I misunderstood what output buffering did, so I didn't think it applied. My bad.

share|improve this question

marked as duplicate by toscho May 8 '12 at 18:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

get_sidebar() just includes a template, so its as dynamic as the string you pass to it. If you want to capture output and store it all as a variable, use output buffering native to PHP.

share|improve this answer

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