Widgetizing Any Page

I copied this page from quirm.net because their comments are closed and there’s a typo in the code that you insert.  The 1st line is missing a “)”.  The rest of the instructions were easy to follow, so I wanted to keep it out there for anyone to use!

Most WordPress themes have widget-ready sidebars but did you know that you can also create widget-ready page templates?

The process is identical to adding a widget area to a sidebar — except that you won’t be making changes to sidebar.php but to another template file. Let’s suppose you want to create a custom Page template with its own widget-ready area.

First, create your custom Page template and call it mytemplate.php. The new template might look something like:

<?php get_header();?>
<div id="page">
<?php get_template_part( 'loop', 'page' );?>
</div>
<?php get_sidebar();
get_footer();

Now, you are going to add a widget-ready area before the main Page content — which, in this example, is generated by <?php get_template_part( 'loop', 'page' );?>.

<?php if ( is_active_sidebar( 'custom' )) :?>
<div class="widget-area">
<?php dynamic_sidebar( 'custom' ); ?>
</div>
<?php endif;?>

The complete template now looks like:

<?php
get_header();?>
<div id="page">
<?php if ( is_active_sidebar( 'custom'  ) :?>
<div class="widget-area">
<?php dynamic_sidebar( 'custom' ); ?>
</div>
<?php endif;?>
<?php get_template_part( 'loop', 'page' );?>
</div>
<?php get_sidebar();
get_footer();

The second step in this process is registering the new widget area. To do this you need to edit your theme’s functions.php file. Remember to make a backup of the file first — just in case anything goes wrong. Add the following to the file after the opening <?php tag:

register_sidebar(array(
	'name'=> 'My Custom Widget Area',
	'id' => 'custom'
));

And that’s it! Your new widget-ready area should now be added to the list of areas displayed in Appearance → Widget. All you have to do now is add one or more widgets and apply the new custom page template to any static Page.

You can use the same method to add widget-ready areas to any template file in your theme — the header, the footer, the main posts page etc.

Happy widgetizing!

Leave a Reply

Your email address will not be published.

Back to top