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.

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.

share|improve this question
    
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

1 Answer 1

up vote 0 down vote accepted
<?php
$bodyclass = "";
// are we on a 'single' page? e.g. a post or page or some other custom post type?
if(is_single()){
    // 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')){
            $bodyclass="wehavesidebarcontentyay";
        }
    }
}

?>
<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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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