How to widgetize a WordPress blog theme

1 comment

If you are using wordpress as you are new,more than likely all of the themes you are looking at using are widgetized. For that special occasion that your WordPress blog theme isn’t widgetized, here’s a simple way of how to hack your theme, and with just a little bit of code, you can widgetize your theme.

As official page from Automattic says:
Instead of register_sidebar() you should use register_sidebars(n) where n is the number of sidebars. Then place the appropriate number in the dynamic_sidebar() function, starting with 1. (There are several other ways to use these function. See the API). You can even give your sidebars names rather than numbers, which lets you maintain a different set of saved sidebars for each theme.





Now You can edit this in the functions.php file. If you don’t have a functions.php file, then you probably don’t have a widgetized theme. Here’s an example of some code that you could put in your functions.php file:

Step 1:-

Look for functions.php (Create one if you don’t have). Insert the following code into it, save and close. We are done with functions.php. Basically what this codes does is – it register the sidebar as dynamic.


<?php
if ( function_exists(‘register_sidebar’) )
register_sidebar();
?>


Step 2:-

Here’s how a typical sidebar.php will look like.

<div id="sidebar">
<h2>Search this site</h2>
<div id="searchdiv">
<form id="searchform" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="s" id="s" size="20"/>
<input name="sbutt" type="submit" value="Go" alt="Submit" />
</form>
</div>
<h2><?php _e(‘Archives’); ?></h2>
<ul><?php wp_get_archives(‘type=monthly’); ?></ul>
<h2><?php _e(‘Categories’); ?></h2>
<ul><?php list_cats() ?></ul>
<h2><?php _e(‘Meta’); ?></h2>
<ul>
<?php wp_register(); ?>
<li><a href="<?php bloginfo(‘rss2_url’); ?>">RSS</a></li>
<li><?php wp_loginout(); ?></li>
</ul>
<?php wp_meta(); ?>
</div>


What you need to do here is to enclose an an-if statement into sidebar.php. The addition of codes are in bold.

<div id="sidebar">
<h2>Search this site</h2>
<div id="searchdiv">
<form id="searchform" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="s" id="s" size="20"/>
<input name="sbutt" type="submit" value="Go" alt="Submit" />
</form>
</div>
//
<?php if ( !function_exists(‘dynamic_sidebar’)
|| !dynamic_sidebar() ) : ?> // added
//
<h2><?php _e(‘Archives’); ?></h2>
<ul><?php wp_get_archives(‘type=monthly’); ?></ul>
<h2><?php _e(‘Categories’); ?></h2>
<ul><?php list_cats() ?></ul>
<h2><?php _e(‘Meta’); ?></h2>
<ul>
<?php wp_register(); ?>
<li><a href="<?php bloginfo(‘rss2_url’); ?>">RSS</a></li>
<li><?php wp_loginout(); ?></li>
</ul>
<?php wp_meta(); ?>
//
<?php endif; ?> // added
//
</div>


You are Done!!
Next PostNewer Post Previous PostOlder Post Home

1 comment

  1. Really awesome blog. Your blog is really useful for me.I am really happy to inform you that its a very nice post and you are explaining it in a very understanding form.Writing services is the better service that helps students for their academic course while giving more and effective tips for writing works.

    ReplyDelete