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 am developing a theme the pages of which (as well as posts) are so different from the home page that I want different widgets on their sidebars.
Is there a way to do it?

I'm calling the sidebar on single.php this way, exactly as it's on index.php:

<?php get_sidebar(); ?>

The sidebar.php is this way:

<?php
/**
 * The Sidebar containing the primary and secondary widget areas.
 ?>

    <aside>
        <ul>

<?php
    /* When we call the dynamic_sidebar() function, it'll spit out
     * the widgets for that widget area. If it instead returns false,
     * then the sidebar simply doesn't exist, so we'll hard-code in
     * some default sidebar stuff just in case.
 */
if ( ! dynamic_sidebar( 'primary-widget-area' ) ) : ?>


    <?php endif; // end primary widget area ?>
    </ul>

<?php
    // A second sidebar for widgets, just because.
    if ( is_active_sidebar( 'secondary-widget-area' ) ) : ?>

        <ul>
                <?php dynamic_sidebar( 'secondary-widget-area' ); ?>
        </ul>

<?php endif; ?>

    </aside>
share|improve this question

1 Answer 1

up vote 3 down vote accepted

Use Conditional Tags to show content only if a certain condition is met.

In your case, you're probably looking to use is_front_page().

<aside>
    <ul>

    <?php
        if ( function_exists( 'dynamic_sidebar' ) ) {
             if ( is_front_page() ) {
                 if ( ! dynamic_sidebar( 'frontpage-widget-area' ) ) {
                     echo '<li>No sidebars for the frontpage.</li>'; // some default output
                 }
             } else {
                 if ( ! dynamic_sidebar( 'primary-widget-area' ) ) {
                     echo '<li>No sidebars for posts/pages.</li>'; // some default output
                 }
             }
        } else {
            echo '<li>Sidebars disabled.</li>'; // some default output
        }
    ?>

    </ul>
</aside>

That's assuming, the two widget areas have been properly registered via register_sidebar() beforehand.

share|improve this answer
    
That's exactly what I was looking for. Worked perfectly. Thank you so much! –  Otavio Vidal Mar 30 '13 at 19: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.