Creating Custom Widget Areas

Some friends have been asking me how I created and/or manage my homepage. As you can tell by the title of this this post, I use a custom widget area for my homepage. This allows me to update content quickly via the drag-and-drop admin interface.  It should be noted that I do use some widgets to display the latest posts from a certain category. You can hard code this functionality, but using a plugin allows you the opportunity to quickly alter settings in the admin UI.  I will go into more detail in the actual tutorial below.  Enjoy!

Creating a Custom Widget Area

function.php for TwentyEleven

skip to below if you are using a theme based off of the TwentyTen Theme

  1. First open the function.php file of your current theme
  2. Sidebars and widget area registration starts on line 365. Find this section of code.
    Screen shot 2011 09 06 at 11.08.36 AM Creating Custom Widget Areas
  3. Copy one declaration for a sidebar (this will save you some time.) This usually starts with,
    register_sidebar( array(
    See the image below as an example.
    Screen shot 2011 09 06 at 11.15.38 AM Creating Custom Widget Areas
  4. Paste this selection at the beginning of this section. This would be above the first, “register_sidebar( array(“
  5. Change the line,
    'name' => __( 'Main Sidebar', 'twentyeleven' ),
    to
    'name' => __( 'Homepage Sidebar', 'twentyeleven' ),
  6. Change the line,
    'id' => 'sidebar-1',
    to (this must be a unique ID/#)
    'id' => 'sidebar-0',
  7. Save and upload this new function.php file
  8. Homepage Sidebar should now show-up in the Widget tab under Appearance in the admin panel.

function.php for TwentyTen

skip to below if you are using a theme based off of the TwentyEleven Theme

  1. First open the function.php file of your current theme
  2. Sidebars and widget area registration starts on line 365. Find this section of code.
    Screen shot 2011 09 06 at 11.49.56 AM Creating Custom Widget Areas
  3. Copy one declaration for a sidebar (this will save you some time.) This usually starts with,
    register_sidebar( array(
  4. Paste this selection at the beginning of this section. This would be above the first, “register_sidebar( array(“
  5. Change the line,
    'name' => __( 'Primary Widget Area', 'twentyten' ),
    to
    'name' => __( 'Homepage Sidebar', 'twentyeleven' ),
  6. Change the line,
    'id' => 'primary-widget-area',
    to (this must be made to a unique ID/#)
    'id' => 'homepage-widget-area',
  7. Change the line,
    'description' => __( 'The primary widget area', 'twentyten' ),
    to
    'description' => __( 'The homepage widget area', 'twentyten' ),
  8. Save and upload this new function.php file
  9. Homepage Sidebar should now show-up in the Widget tab under Appearance in the admin panel.

Adding a New Widget Area into Theme Files

For TwentyEleven Based Themes

  1. Drop this code in any theme file to add the new widget area,
    <? if ( is_home() ) : ?>
    <div id="homepageWidget" role="complementary">
    <?php if ( ! dynamic_sidebar( 'sidebar-0' ) ) : ?>
    </div><!-- #homepageWidget -->
    <?php endif ; ?>
    <?php endif ; ?>
  2. If you have changed the homepage to display a static page, try the conditional “is_front_page()” instead.

  3. As you can probably already tell, this bit of code checks to see if the current page/post/location is the homepage. If this condition is true, then it will launch the homepage sidebar.
  4. For TwentyEleven try adding this code to the index.php or content.php. In the index, start add before or after the loop. In content, try adding it to the beginning. If you have messed with your default homepage settings, you may need to use a file other than content.php.

For TwentyTen Based Themes

Adding a widget area usually depends on exactly where you would like it to display on the live site. This could mean bypassing the main loop for just the homepage. A good place to start in the TwentyTen theme is in the header.php or the single.php files. In the TwentyEleven Theme try the index.php file. The familiar loop.php from the TwentyTen theme no longer exists because the TwentyEleven Theme now displays a wide range of posts depending on their content and/or template. Therefore, if you are working with a TwentyEleven based theme, start with index.php

  1. Drop this code in any theme file to add the new widget area,
    <? if ( is_home() ) : ?>
    <div id="homepage-widget" role="complementary">
    < ?php dynamic_sidebar( 'homepage-widget-area' ); ?>
    </div><!-- #homepage-widget .widget-area -->
    <?php endif ; ?>
  2. As you can probably already tell, this bit of code checks to see if the current page/post/location is the homepage. If this condition is true, then it will launch the homepage sidebar.
  3. For TwentyTen try adding this code to the header.php, single.php, or archive.php.
  4. If you have changed the homepage to display a static page, try the conditional “is_front_page()” instead.

Remember, if your site’s homepage is a static page and not a stream of posts, this could effect the results. Again, try “is_front_page()” for best results.

At this point you should now have a drag-and-drop content area for any location on the site. This makes thing easy to update and allows you to quickly test new widgets. Don’t forget to style these! Good luck and post any questions in the comment section below.

Posted in Blog, Tutorial, WordPress | Tagged , , , , , , | 9 Comments

9 Responses to Creating Custom Widget Areas

  1. I’ve successfully created the “Homepage Widget” but somehow I’m not able to get any of the widgets to show up in my homepage. I’ve created a static home page and tried pasting your code into both content.php and index.php. Is there a specific place in which I need to paste that code? and should the divs be inside the ? Thanks!

  2. Rik says:

    Hy Geeb,

    I use the twentyten theme, and I would like the new widget are was displayed above the first post.
    How should I specify that in the code?

    thank you in advance

    • Geeb says:

      Hey Rik,

      Check out the last section of this tutorial. Under, “For TwentyTen based Themes”, check out the code in that section. You would remove the conditional “if” statement from this code, but essentially all you need to do is declare your custom code in the theme’s template (.php) files. Try adding this widget area to the end of the header.php, or add it to the beginning of archive.php, category.php, page.php, and single.php to name a few. It really depends on where “above” the posts you want them to exists. header.php is a great place to test because all templates include this file, but not all locations on the site use, say the archive.php file. If you need more help, send me an e-mail.

  3. Rik says:

    I just got it!!

    Thank you very much indeed :P

    • Geeb says:

      No problem Rik. If you need any more help, or if you have any ideas for a tutorial you may want me to tackle, post it here. I’m always looking for the next problem to solve. Seriously, drop a line! Thanks for stopping in.

  4. André says:

    Hello Geeb,

    I am with a problem similiar to Rik.

    I’ve created a new menu, and I’d like display it just above the “wrapper” as it is on http://www.huffingtonpost.com/

    Actually I think it is into the wrapper on huffpost, but when I introduce the code in header.php it appear into the “white content”, and I’d like it was OVER the “white content” just as in huffpost with the body bg collor.

    When I introduce the code out wrapper, it still is displayed in the “white content” and very untidy.

    So, if you can solve this problem, I’d appreciate a lot.

    The menu css is the same in twenty ten theme usually. I just did a copy changing access to access2.

  5. Bonita says:

    Thanks so much! That was much simpler and easier than I had imagined!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Like this post?

Subscribe to the RSS Feed or to Email Updates, right now!