0

Am I violating any best practices if I simply display a sidebar via dynamic_sidebar(); without checking if it's active first?

1

No, but if you're adding extra markup around the dynamic_sidebar call such as a container, it might look odd if there are no widgets.

Test it out and if it works for you then it should be fine

0

I would suggest you use the conditional in conjunction with some other functions and styles, to maintain an overall good look for your theme.

For example, if your sidebar is inside an aside and the `aside is taking 300px of your theme's width, then you can use something like this:

if (!is_active_sidebar()) {
    wp_add_inline_style('my-style',' #content { width: 1200px }');
}

And this is your code to output the sidebar:

<?php if (is_active_sidebar() { ?>
    <div id="aside"><?php
        dynamic_sidebar();?>
    </div><?php
} ?>

While your normal content is:

#content {
    width:900px;
}
#aside {
    width: 300px;
}

This way, if your sidebar is empty, the rest of the document will fill the empty space, which is one of the steps to make a responsive theme.

This is basically what some themes like twentyseventeen do. You can also change the $content_width depending on whether the sidebar is active or not:

if (is_active_sidebar()) { 
    $content_width = 900;
} else {
    $content_width = 1200;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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