Skip to content

Instantly share code, notes, and snippets.

Created October 24, 2011 11:58
Show Gist options
  • Save anonymous/1308851 to your computer and use it in GitHub Desktop.
Save anonymous/1308851 to your computer and use it in GitHub Desktop.
Add dynamic widget areas depending on the page
register_sidebar( array(
'name' => __( 'Homepage Widget area', 'twentyeleven' ),
'id' => 'sidebarhp_widget_area',
'description' => __( 'An optional widget area that appears only on your homepage', 'twentyeleven' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => "</aside>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
register_sidebar( array(
'name' => __( 'Category widget area', 'twentyeleven' ),
'id' => 'sidebarcategory_widget_area',
'description' => __( 'An optional widget that appears only on category pages', 'twentyeleven' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => "</aside>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
<?php
// This widget area will appear on the homepage only
if ( is_active_sidebar( 'sidebarhp_widget_area' ) && is_home() ) : ?>
<div id="sidebarhp" class="widget-area">
<ul>
<?php dynamic_sidebar( 'sidebarhp_widget_area' ); ?>
</ul>
</div>
<?php endif;
// This widget area will appear on the category pages
if ( is_active_sidebar( 'sidebarcategory_widget_area' ) && is_category() ) : ?>
<div id="sidebarhp" class="widget-area">
<ul>
<?php dynamic_sidebar( 'sidebarcategory_widget_area' ); ?>
</ul>
</div>
<?php endif;
?>
@kristinabressler
Copy link

I couldn't get mine to work. I can't get the second widget to display in a certain post.

Here's my code:

<?php  
//for rest of posts
if (is_active_sidebar('blog_widget_area') ) : ?>
    <div id="secondary" class="blog_widget_area bordered" role="complementary">
        <?php dynamic_sidebar( 'blog_widget_area' ); ?>
    </div><!-- #secondary .widget-area -->
<?php endif;

//for specific post "Loyalty Program Trends in the Restaurant Industry"
if (is_active_sidebar('loyalty_programs_widget_area') && is_single('4063') ) : ?>
        <div id="secondary" class="blog_widget_area bordered" role="complementary">
        <?php dynamic_sidebar( 'loyalty_programs_widget_area' ); ?>
        </div>      
<?php endif;

?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment