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?


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.


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.

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