Widgetise your template for wordpress
I’ve had a few comments on the article about displaying recent posts where you want. The solution I gave in the post work but I’d like to present here anĀ alternative solution that might be better in some cases. Rather than calling the recent post function we are going to use the recent post widget.
One person from the comments wanted to display recent post in his 404 page. The problem is, in the default 404.php there isn’t any widgetized area:
<div id="content" class="narrowcolumn">
<h2 class="center">Error 404 - Not Found</h2>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
First we need to create the place to display the widgets. Thanks to the Wordpress API we can do this in 2 lines:
<div id="content" class="narrowcolumn">
<h2 class="center">Error 404 - Not Found</h2>
<?php if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar("404 widget") ) : ?>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Now we have to tell wordpress something like “Hey look, you can put widgets in here as well!”. To do that we use the function.php file and place in it those lines:
register_sidebar(array(
‘name’ => ‘404 widget’,
‘before_widget’ => ”,
‘after_widget’ => ”,
‘before_title’ => ‘<h3 class="widgettitle">’,
‘after_title’ => ‘</h3>’,
));
I think it is self explanatory. The name is the name parameter we placed in the 404.php code, before and after widget are optional html you could need, same for before/after title.
Now you’ve done this go to your admin panel>design>widgets, select the new widget from the dropdown list on the right hand side and click show. Add the widgets you want and press save. Here is you new custom 404 page. You can add everything from search box to tag cloud.


