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 want to display a dynamic sidebar on all pages, and a different sidebar on the homepage. There are also particular pages where I don't want to display either of them.

This code is not hiding the unwanted sidebar off of the Homepage; both sidebars show. How can I hide 'multipackage' from the homepage and only display 'singlepackage'? (I guess I thought that the elseif would take care of that. I have also tried just 'else' but I get a syntax error).

<?php
    if(!function_exists('dynamic_sidebar') 
        || !dynamic_sidebar('multipackage'));

    elseif (is_page_template('page-homepage.php')) {
        dynamic_sidebar('singlepackage');

        // also need to hide both sidebars from Page IDs 4 and 5

        }

    ?>

FWIW I have read up on the codex http://codex.wordpress.org/Function_Reference/dynamic_sidebar , this site, and other resources such as http://dreamwhisperdesigns.com/genesis-tutorials/load-sidebar-specific-pages/ (I am not an experienced php developer and syntax is where I often fail, if that's not already obvious!)

I would really like to avoid using a plugin for this. Thanks everyone in advance.

share|improve this question

3 Answers 3

up vote 1 down vote accepted

I want to display a dynamic sidebar on all pages, and a different sidebar on the homepage. There are also particular pages where I don't want to display either of them.

This code is not hiding the unwanted sidebar off of the Homepage; both sidebars show. How can I hide 'multipackage' from the homepage and only display 'singlepackage'? (I guess I thought that the elseif would take care of that. I have also tried just 'else' but I get a syntax error).

Assuming these conditions:

  • singlepackage sidebar displays on the Site Front Page (or do you mean the Blog Posts Index?)
  • multipackage sidebar displays on static pages
  • multipackage sidebar does not display on static page with ID $id

This is the basic implementation:

<?php
if ( is_front_page() ) {
    // This is the Site Front Page,
    // display singlepackage
    dynamic_sidebar( 'singlepackage' );
} else if ( is_page() && ! is_page( $id ) ) {
    // This is a static page,
    // but NOT static page with ID $id;
    // display multipackage
    dynamic_sidebar( 'multipackage' );
}
?>

Note: if you want singlepackage to display when on the Blog Posts Index as opposed to on the Site Front Page, you'll want to use is_home():

<?php
if ( is_home() ) {
    // This is the Site Front Page,
    // display singlepackage
    dynamic_sidebar( 'singlepackage' );
} else if ( is_page() && ! is_page( $id ) ) {
    // This is a static page,
    // but NOT static page with ID $id;
    // display multipackage
    dynamic_sidebar( 'multipackage' );
}
?>

If you want to display default content (i.e. content that displays if no Widgets are applied to the specified widget area), then you'll need to use the if ( ! dynamic_sidebar( 'singlepackage' ) ), like so:

<?php
if ( is_front_page() ) {
    // This is the Blog Posts Index,
    // display singlepackage
    if ( ! dynamic_sidebar( 'singlepackage' ) ) {
        // default content goes here
    }
} else if ( is_page() && ! is_page( $id ) ) {
    // This is a static page,
    // but NOT static page with ID $id;
    // display multipackage
    if ( ! dynamic_sidebar( 'multipackage' ) ) {
        // default content goes here
    }
}
?>
share|improve this answer
    
Thank you so much, it is starting to come together in my brain!!! –  cindyg Nov 20 '12 at 20:54
    
The way I have to think of things: figure out the logic in human terms (I want X to happen if Y but not Z, etc.), and then translate those human terms into code. :) –  Chip Bennett Nov 20 '12 at 20:55

What your code says in English is this:

If there is no dynamic_sidebar function OR there is no sidebar called multipackage ELSE IF this is the home page template THEN show the singlepackage sidebar.

... so you can see that there is no "THEN" part of the first condition.

<?php
if (function_exists('dynamic_sidebar') && dynamic_sidebar('singlepackage') && is_page_template('page-homepage.php')) {
    dynamic_sidebar('singlepackage');
elseif( function_exists('dynamic_sidebar') && dynamic_sidebar('multipackage'));
    dynamic_sidebar('multipackage');
}
?>

This is saying IF the function dynamic_sidebar exists AND there is a singlepackage sidebar AND this is the home page template THEN show the singlepackage sidebar. ELSE IF the function dynmaic_sidebar exists AND there is a multipackage sidebar THEN show the multipackage sidebar.

share|improve this answer
    
You can simplify things significantly by getting rid of the function_exists( 'dynamic_sidebar' ) conditional. The dynamic_sidebar() function has existed since WordPress 2.9 (or was it 2.8?). EDIT Whoops. Make that WordPress 2.2 –  Chip Bennett Nov 20 '12 at 20:23
    
True. I didn't optimize this at all, @cindyg. But hopefully seeing this written out this you'll be able to understand it more. Feel free to remove both instances of function_exists( 'dynamic_sidebar' ). –  developdaly Nov 20 '12 at 20:30
    
@developdaly thank you - that makes great sense to me. But when I try it, I get an error on the elseif line. I tried messing around with it but just messed it up more. Like I said my sytax skills are lacking so it's hard for me to figure these things out. –  cindyg Nov 20 '12 at 20:33
    
@Chip Bennett - thanks for the info. I'm a little confused why the codex page for dynamic_sidebar() does not mention that it's deprecated. –  cindyg Nov 20 '12 at 20:35
    
The Codex doesn't mention that dynamic_sidebar() is deprecated because dynamic_sidebar() isn't deprecated. :) –  Chip Bennett Nov 20 '12 at 20:36

Unfortunately none of the help here worked for my particular situation for one reason or another (was running into a lot of syntax errors and I don't know why). This is what ended up working:

<?php 
        if ( is_front_page() ) {
            dynamic_sidebar( 'singlepackage' );
        }
        elseif ( is_page(array('827','828','829','830') )) {    
            dynamic_sidebar('multipackage');
        } 
        else {
        }
    ?> 

which I sort of pieced together from the examples under "Variable Sidebar Content" on - http://codex.wordpress.org/Conditional_Tags

share|improve this answer
1  
I'm quite happy that you got it working, but this is, in essence, the exact answer I gave you. :) –  Chip Bennett Nov 26 '12 at 23:30
    
ahhh, sorry @ChipBennett - that code really had my head spinning. Thank you again for your help, I finally marked your answer :) –  cindyg Mar 5 '13 at 5:06

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.