I've got a registered sidebar called "my-header" that affects the absolute positioning of elements below it in the markup.

So I need to execute a query in header.php to determine if the sidebar is present for the current post, and write out a class identifier to my theme's body tag. I'll use this css class to adjust absolute positioning of elements accordingly.

Is there a method that can be called, separately from the method that's used to display the sidebar, to determine if the post has widget content for the "my-sidebar" widget? For example, one that just returns true/false?

After looking through widgets.php, I tried using is_active_sidebar('my-header') but it returns true for all pages. I need a function that accepts the post as an argument. Otherwise, if none exists, I suppose I'll create my own function.

  • You might want to take a look at Justin Tadlock's My Snippets plugin, wordpress.org/extend/plugins/my-snippets – Manzabar Apr 6 '11 at 16:33
  • @Manzabar: looks nifty. I've had this on my list to add to my theme for some time. I appreciate the reference. – Scott B Apr 6 '11 at 16:38
  • 2
    Could you please explain this part - if the post has widget content for the "my-sidebar" widget . Posts and widgets are completely different things, they do not intersect, unless you use some custom context-aware widget. – Rarst May 30 '11 at 23:03
$bodyclass = "";
// are we on a 'single' page? e.g. a post or page or some other custom post type?
    // is this a post of type 'page'? or is it a blogpost?
    global $post;
    if($post->post_type == 'page'){
        // good now to check if we have a sidebar with active content
        if( is_active_sidebar('my-header')){

<body <?php body_class($bodyclass); ?>>

Though I'm sure if you have body_class on your body tag then you already have the needed css classes and selectors to do this without the PHP code.

| improve this answer | |

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.