<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ZENVERSE &#187; widget</title>
	<atom:link href="http://zenverse.net/tag/widget/feed/" rel="self" type="application/rss+xml" />
	<link>http://zenverse.net</link>
	<description>Design and Web Development</description>
	<lastBuildDate>Sun, 13 May 2012 15:26:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Creating Dynamic Sidebars (widget-ready theme) in WordPress</title>
		<link>http://zenverse.net/creating-dynamic-sidebars-in-wordpress/</link>
		<comments>http://zenverse.net/creating-dynamic-sidebars-in-wordpress/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 17:13:55 +0000</pubDate>
		<dc:creator>Zen</dc:creator>
				<category><![CDATA[Wordpress Development]]></category>
		<category><![CDATA[dynamic sidebar]]></category>
		<category><![CDATA[sidebar]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[widget]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://zenverse.net/?p=39</guid>
		<description><![CDATA[A step-by-step guide to create dynamic sidebars for your wordpress theme, making your theme widget-ready.]]></description>
			<content:encoded><![CDATA[<h3>What is dynamic sidebar?</h3>
<p>Dynamic sidebar is &#8220;dynamic&#8221;, it simply means that your sidebar can be customised easily by adding / removing widgets in admin panel. If your theme has dynamic sidebar, you can called it a widget-ready theme.</p>
<h3>Step-by-step guide to create dynamic sidebar</h3>
<h4>1. Make sure the HTML codes for your (every) widgets follow a certain format</h4>
<p>This is an example:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code1'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p391"><td class="code" id="p39code1"><pre class="html" style="font-family:monospace;">&lt;div class=&quot;widget&quot;&gt;
    &lt;h2&gt;Categories&lt;/h2&gt;
      &lt;ul&gt;
        &lt;li&gt;Cat #1&lt;/li&gt;
        &lt;li&gt;Cat #2&lt;/li&gt;
      &lt;/ul&gt;
  &lt;/div&gt;
&nbsp;
&lt;div class=&quot;widget&quot;&gt;
    &lt;h2&gt;About&lt;/h2&gt;
    About me About me About me
  &lt;/div&gt;</pre></td></tr></table></div>

<p>In the above example, we can see that every widget is surrounded by &lt;div class=&#8221;widget&#8221;> and its title is surrounded by &lt;h2> tags.</p>
<h4>2. Look for functions.php</h4>
<p>Second, look for functions.php in your theme folder. Create one if it is not there.</p>
<h4>3. Register the dynamic sidebar</h4>
<p>You need to let wordpress know that your theme has a dynamic sidebar. Place these lines of codes into your functions.php:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code2'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p392"><td class="code" id="p39code2"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/function_exists"><span style="color: #990000;">function_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'register_sidebar'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
register_sidebar<span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'sidebar'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'before_widget'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'after_widget'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'before_title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'after_title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The function uses an array that contains 5 items.<br />
<strong>Name</strong> &#8211; Name of your sidebar<br />
<strong>before_widget</strong> &#8211; the html tags that wraps an invidual widget, eg: div. Put only the opening tag here.<br />
<strong>after_widget</strong> &#8211; if your &#8220;before_widget&#8221; uses a &lt;div> tag, you should use &lt;/div> here<br />
<strong>before_title and after_title</strong> &#8211; html tags surrounding the widget&#8217;s title</p>
<h4>4. Registering the dynamic sidebar : Examples</h4>
<p>If your sidebar look like this:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code3'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p393"><td class="code" id="p39code3"><pre class="html" style="font-family:monospace;">&lt;div class=&quot;rightsidebar&quot;&gt;
&nbsp;
  &lt;div class=&quot;widget&quot;&gt;
    &lt;h2&gt;Recent Posts&lt;/h2&gt;
      &lt;ul&gt;
        &lt;li&gt;Recent Post #1&lt;/li&gt;
        &lt;li&gt;Recent Post #2&lt;/li&gt;
        &lt;li&gt;Recent Post #3&lt;/li&gt;
      &lt;/ul&gt;
  &lt;/div&gt;
&nbsp;
  &lt;div class=&quot;widget&quot;&gt;
    &lt;h2&gt;Categories&lt;/h2&gt;
      &lt;ul&gt;
        &lt;li&gt;Cat #1&lt;/li&gt;
        &lt;li&gt;Cat #2&lt;/li&gt;
        &lt;li&gt;Cat #3&lt;/li&gt;
      &lt;/ul&gt;
  &lt;/div&gt;
&nbsp;
&lt;/div&gt;&lt;!--/rightsidebar--&gt;</pre></td></tr></table></div>

<p>This is how you register your sidebar. (NOTE: ignore the div that wraps the whole sidebar first, we are currently dealing with codes for every single widget)</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code4'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p394"><td class="code" id="p39code4"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/function_exists"><span style="color: #990000;">function_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'register_sidebar'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
register_sidebar<span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'sidebar'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'before_widget'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;div class=&quot;widget&quot;&gt;'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'after_widget'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;/div&gt;'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'before_title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;h2&gt;'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'after_title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;/h2&gt;'</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Another example: If your sidebar look like this :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code5'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p395"><td class="code" id="p39code5"><pre class="html" style="font-family:monospace;">&lt;div class=&quot;rightsidebar&quot;&gt;
&nbsp;
  &lt;div class=&quot;widget_top&quot;&gt;
    &lt;div class=&quot;widget_title&quot;&gt;Recent Posts&lt;/div&gt;
      &lt;ul&gt;
        &lt;li&gt;Recent Post #1&lt;/li&gt;
        &lt;li&gt;Recent Post #2&lt;/li&gt;
        &lt;li&gt;Recent Post #3&lt;/li&gt;
      &lt;/ul&gt;
  &lt;/div&gt;
  &lt;div class=&quot;widget_foot&quot;&gt;&lt;/div&gt;
&nbsp;
&nbsp;
  &lt;div class=&quot;widget_top&quot;&gt;
    &lt;div class=&quot;widget_title&quot;&gt;Categories&lt;/div&gt;
      &lt;ul&gt;
        &lt;li&gt;Cat #1&lt;/li&gt;
        &lt;li&gt;Cat #2&lt;/li&gt;
        &lt;li&gt;Cat #3&lt;/li&gt;
      &lt;/ul&gt;
  &lt;/div&gt;
  &lt;div class=&quot;widget_foot&quot;&gt;&lt;/div&gt;
&nbsp;
&nbsp;
&lt;/div&gt;&lt;!--/rightsidebar--&gt;</pre></td></tr></table></div>

<p>This is how you register your sidebar.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code6'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p396"><td class="code" id="p39code6"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/function_exists"><span style="color: #990000;">function_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'register_sidebar'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
register_sidebar<span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'sidebar'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'before_widget'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;div class=&quot;widget_top&quot;&gt;'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'after_widget'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;/div&gt;&lt;div class=&quot;widget_foot&quot;&gt;&lt;/div&gt;'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'before_title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;div class=&quot;widget_title&quot;&gt;'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'after_title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;/div&gt;'</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h4>5. Making the sidebar dynamic</h4>
<p>Open sidebar.php. Lets say we have this in our sidebar.php :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code7'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p397"><td class="code" id="p39code7"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;rightsidebar&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;widget&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>h2<span style="color: #339933;">&gt;</span>Recent Posts<span style="color: #339933;">&lt;/</span>h2<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>ul<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> the_post<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?php the_permalink(); ?&gt;&quot;</span> rel<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bookmark&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?</span>php the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;&lt;/</span>li<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
      <span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;widget&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>h2<span style="color: #339933;">&gt;</span>Categories<span style="color: #339933;">&lt;/</span>h2<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>ul<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> wp_list_categories<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'show_count=1&amp;title_li='</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
      <span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;&lt;!--/</span>rightsidebar<span style="color: #339933;">--&gt;</span></pre></td></tr></table></div>

<p>Now we are going to make it dynamic. Leaves only the sidebar wrapper &lt;div> there.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code8'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p398"><td class="code" id="p39code8"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;rightsidebar&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><a href="http://www.php.net/function_exists"><span style="color: #990000;">function_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dynamic_sidebar'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>dynamic_sidebar<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sidebar&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;&lt;!--/</span>rightsidebar<span style="color: #339933;">--&gt;</span></pre></td></tr></table></div>

<blockquote><p>Please note that the word &#8220;sidebar&#8221; inside the brackets in <strong>!dynamic_sidebar(&#8220;sidebar&#8221;)</strong> is the name you used when registering the sidebar. </p></blockquote>
<p>It&#8217;s done. Your theme is now widget-ready. However, you might want to make it better.</p>
<h4>5. Prepare your theme for users that don&#8217;t use widgets</h4>
<p>Minority of people don&#8217;t use widgets in WordPress, we have to include some basic features for them.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code9'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p399"><td class="code" id="p39code9"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;rightsidebar&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><a href="http://www.php.net/function_exists"><span style="color: #990000;">function_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dynamic_sidebar'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>dynamic_sidebar<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sidebar&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>
<span style="color: #666666; font-style: italic;">// these codes will be run if they don't use any widget</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// I take the sidebars codes from above example</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;widget&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>h2<span style="color: #339933;">&gt;</span>Recent Posts<span style="color: #339933;">&lt;/</span>h2<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>ul<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> the_post<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?php the_permalink(); ?&gt;&quot;</span> rel<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bookmark&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?</span>php the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;&lt;/</span>li<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
      <span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;widget&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>h2<span style="color: #339933;">&gt;</span>Categories<span style="color: #339933;">&lt;/</span>h2<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>ul<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> wp_list_categories<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'show_count=1&amp;title_li='</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
      <span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;&lt;!--/</span>rightsidebar<span style="color: #339933;">--&gt;</span></pre></td></tr></table></div>

<p>Look, we have now included 2 basic features for them: Recent Posts and Categories.</p>
<h4>6. So, your theme has 2 or more sidebars?</h4>
<p>Easy, lets register all of them in functions.php :</p>
<p>Assume you have 2 sidebars with same format  :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code10'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3910"><td class="code" id="p39code10"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/function_exists"><span style="color: #990000;">function_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'register_sidebars'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
register_sidebars<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Sidebar%d'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'before_widget'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;div class=&quot;widgetobj&quot;&gt;'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'after_widget'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;/div&gt;'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'before_title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;div class=&quot;widgettitle&quot;&gt;&lt;h3&gt;'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'after_title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;/h3&gt;&lt;/div&gt;'</span><span style="color: #339933;">,</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<blockquote><p>Please note that:<br />
- The function used here is <strong>register_sidebar<u>s</u></strong> instead of <strong>register_sidebar</strong>.<br />
-  Change the number &#8220;2&#8243; to the number of sidebars your theme has.<br />
- The %d will be auto replaced by numbers. If you register 2 sidebars, they will be known as <strong>Sidebar1</strong> and <strong>Sidebar2</strong> (in this example). You are going to use the names later.
</p></blockquote>
<p>This is what we have in <strong>sidebar.php</strong> :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code11'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3911"><td class="code" id="p39code11"><pre class="html" style="font-family:monospace;">&lt;div class=&quot;sidebar_1_wrapper&quot;&gt;
&nbsp;
  &lt;div class=&quot;widget&quot;&gt;
    &lt;h2&gt;Recent Posts&lt;/h2&gt;
      &lt;ul&gt;
        &lt;li&gt;Recent Post #1&lt;/li&gt;
        &lt;li&gt;Recent Post #2&lt;/li&gt;
        &lt;li&gt;Recent Post #3&lt;/li&gt;
      &lt;/ul&gt;
  &lt;/div&gt;  
&nbsp;
&lt;/div&gt;&lt;!--/sidebar_1_wrapper--&gt;
&nbsp;
&lt;div class=&quot;sidebar_2_wrapper&quot;&gt;
&nbsp;
  &lt;div class=&quot;widget&quot;&gt;
    &lt;h2&gt;Recent Posts&lt;/h2&gt;
      &lt;ul&gt;
        &lt;li&gt;Recent Post #1&lt;/li&gt;
        &lt;li&gt;Recent Post #2&lt;/li&gt;
        &lt;li&gt;Recent Post #3&lt;/li&gt;
      &lt;/ul&gt;
  &lt;/div&gt;  
&nbsp;
&lt;/div&gt;&lt;!--/sidebar_2_wrapper--&gt;</pre></td></tr></table></div>

<p>Now we make them dynamic :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code12'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3912"><td class="code" id="p39code12"><pre class="html" style="font-family:monospace;">&lt;div class=&quot;sidebar_1_wrapper&quot;&gt;
	&lt;?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(&quot;Sidebar1&quot;) ) : ?&gt;
	&lt;?php endif; ?&gt;
&lt;/div&gt;&lt;!--/sidebar_1_wrapper--&gt;
&nbsp;
&lt;div class=&quot;sidebar_2_wrapper&quot;&gt;
  	&lt;?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(&quot;Sidebar2&quot;) ) : ?&gt;
	&lt;?php endif; ?&gt;
&lt;/div&gt;&lt;!--/sidebar_2_wrapper--&gt;</pre></td></tr></table></div>

<blockquote><p>Please note that:<br />
- The name of sidebars you registered were used here.<br />
- Follow step 5 again to prepare your sidebars for users that don&#8217;t use widgets<br />
- If your sidebars do not have same design/format, register them seperately (as in step 3) and make them dynamic like the example just above this.
</p></blockquote>
<h3>That&#8217;s all</h3>
<p>That is all for this tutorial. Please point to me any part that is confusing so that I can make this tutorial better.</p>
<p>Thank you for reading.</p>
]]></content:encoded>
			<wfw:commentRss>http://zenverse.net/creating-dynamic-sidebars-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

