How to display different widgets on specific pages, hide from other pages

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