<?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>eric lightbody &#187; php</title>
	<atom:link href="http://www.ericlightbody.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ericlightbody.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Mon, 26 Apr 2010 18:04:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Adding multiple sidebars (and other elements) to your wordpress theme</title>
		<link>http://www.ericlightbody.com/2009/adding-multiple-sidebars-and-other-elements-to-your-wordpress-theme/</link>
		<comments>http://www.ericlightbody.com/2009/adding-multiple-sidebars-and-other-elements-to-your-wordpress-theme/#comments</comments>
		<pubDate>Fri, 01 May 2009 03:28:59 +0000</pubDate>
		<dc:creator>Eric Lightbody</dc:creator>
				<category><![CDATA[web-development]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[theme]]></category>

		<guid isPermaLink="false">http://www.ericlightbody.com/?p=158</guid>
		<description><![CDATA[I&#8217;m going to show you (you, being the wordpress theme developer or modifier) how to add elements to your blog that will change based on which page you are on. A great example of this is changing which button is on for your navigation to indicate which section is currently active. I will also show [...]]]></description>
			<content:encoded><![CDATA[<p><img class="align-left img-border" title="WordPress Logo" src="http://www.ericlightbody.com/wp-content/uploads/2009/04/wordpress-logo1.png" alt="WordPress Logo" width="48" height="48" />I&#8217;m going to show you (you, being the wordpress theme developer or modifier) how to add elements to your blog that will change based on which page you are on.  A <a href="http://wordpress.org/">great example of this</a> is changing which button is on for your navigation to indicate which section is currently active.   I will also show you how to get multiple widgetized sidebars rocking for your theme.  I&#8217;m going to make the assumption that you already know what <a href="http://codex.wordpress.org/Pages">wordpress pages</a> are and how <a href="http://codex.wordpress.org/User:Lastnode/WordPress_CMS">wordpress can act as a cms</a>.  I will also assume you know how to <a href="http://codex.wordpress.org/Widgetizing_Themes">widgetize your theme</a>. Onwards.<span id="more-158"></span></p>
<h3>Creating multiple dynamic sidebars</h3>
<p>Let&#8217;s take care of a simple task first, creating multiple sidebars.  Normally for a standard wordpress theme you will have this function call to register your dynamic sidebar in your theme&#8217;s function.php file:</p>
<pre class="brush: php;">
if ( function_exists('register_sidebar') )
register_sidebar();
</pre>
<p>That needs to go away.  The code below allows you to create multiple sidebars and name each one uniquely.  Just simply rename &#8216;sidebar#&#8217; and put in as many sidebars as you like.</p>
<pre class="brush: php;">
if ( function_exists('register_sidebar') ) {
register_sidebar(array('name'=&gt;'sidebar1'));
register_sidebar(array('name'=&gt;'sidebar2'));
register_sidebar(array('name'=&gt;'sidebar3'));
}
</pre>
<p>Here is an example of wordpress 2.7.1 using multiple sidebars in the widget screen:</p>
<p><img class="img-border" src="http://www.ericlightbody.com/wp-content/uploads/2009/04/multiple-sidebars-with-widets.png" alt="Multiple Sidebars With Widets" width="317" height="312" /></p>
<p>There is a catch with this.  With multiple sidebars, most likely you are going to want to add the same widget to multiple sidebars.  Some widgets allow you to have multiple instances of the widget on your sidebar(s).  These widgets have a dropdown box in the admin section that allows you to select how many widgets you would like:</p>
<p><img class="img-border" src="http://www.ericlightbody.com/wp-content/uploads/2009/04/widget-selector.png" alt="Widget Selector" width="378" height="95" /></p>
<p>However, most do not.  In this case you need to find the php code for the plugin that initializes the widget.  You will be looking for the <em>register_sidebar_widget</em> call.  For instance, in the <a href="http://wordpress.org/extend/plugins/delicious-cached/">delicious cached</a> plugin, I found this function call:</p>
<pre class="brush: php;">
register_sidebar_widget('Delicious Cached++', 'widget_deliciouspp');
</pre>
<p>All that you need to do is duplicate that line for as many widgets as you need and change the name in the first parameter.  My php code now looks like this:</p>
<pre class="brush: php;">
register_sidebar_widget('Delicious Cached++ 1', 'widget_deliciouspp');
register_sidebar_widget('Delicious Cached++ 2', 'widget_deliciouspp');
register_sidebar_widget('Delicious Cached++ 3', 'widget_deliciouspp');
register_sidebar_widget('Delicious Cached++ 4', 'widget_deliciouspp');
</pre>
<p>Now you should notice your admin interface show multiple instances of the widget like this:</p>
<p><img src="http://www.ericlightbody.com/wp-content/uploads/2009/04/multiple-widgets.png" alt="Multiple Widgets" width="255" height="174" /></p>
<h3>Displaying different sidebars depending what page or subpage you are on</h3>
<p>Now comes the fun part.  Let&#8217;s get those sidebars showing up when you want them to.  You&#8217;re going to want to add this code to your functions.php file.  I won&#8217;t explain what it does until the next part (trust me) (also, thanks to Alberto for this post: <a href="http://www.altrugon.com/php/get-the-top-parent-page-in-wordpress/">http://www.altrugon.com/php/get-the-top-parent-page-in-wordpress/</a>)</p>
<pre class="brush: php;">
function getTopParentPostName($myid) {
$mypage = get_page($myid);

if ($mypage-&gt;post_parent == 0){
return $mypage-&gt;post_name;
}
else{
return getTopParentPostName($mypage-&gt;post_parent);
}
}

function is_tree( $p_name ) {    // $p_name = The page we're looking for pages underneath
global $post;       // We load this as we're outside of the post
$top_post_name = getTopParentPostName($post);

if ( $p_name == $top_post_name ) return true;
else return false;
}
</pre>
<p>Now, normally in order to register a dynamic sidebar for your theme, you just do this in sidebar.php (according to <a href="http://codex.wordpress.org/Widgetizing_Themes">wordpress</a>):</p>
<pre class="brush: php;">
if ( function_exists('register_sidebar') )
register_sidebar();
</pre>
<p>You will be changing it to something like this where main page title # is your main page title name and sidebar# matches your sidebar names as discussed earlier:</p>
<pre class="brush: php;">
if ( is_tree('main page title 1') ) {
if (!function_exists('dynamic_sidebar') || !dynamic_sidebar( 'sidebar1') ) { $generic_sidebar = true; }
}
elseif ( is_tree('main page title 2')) {
if (!function_exists('dynamic_sidebar') || !dynamic_sidebar( 'sidebar2') ) { $generic_sidebar = true; }
}
elseif ( is_tree('main page title 3' )) {
if (!function_exists('dynamic_sidebar') || !dynamic_sidebar( 'sidebar3') ) { $generic_sidebar = true; }
}
</pre>
<p>Here is what&#8217;s happening: the is_tree call checks to see if the current page or subpage you are on is in the family &#8220;main page title #&#8221;.  If it is, then it registers the appropriate sidebar.  That&#8217;s it!  So, sidebars are cool and all, but what about other elements?</p>
<h3>Displaying other elements depending what page or subpage you are on</h3>
<p>This part is easy now that we&#8217;ve got the sidebars done.  Let&#8217;s use an unordered list for top navigation as an example.  I am just adding a class of &#8220;over&#8221; to the active menu item.  You can see that the is_tree function is called again.</p>
<pre class="brush: xml;">
&lt;ul&gt;
&lt;li &lt;?php if ( is_tree('need-a-website') ) echo ' class=&quot;over&quot;'; ?&gt;&gt;&lt;a href=&quot;http://www.google.com&quot;&gt;Google&lt;/a&gt;&lt;/li&gt;
&lt;li &lt;?php if ( is_tree('blogging') ) echo ' class=&quot;over&quot;'; ?&gt;&gt;&lt;a href=&quot;http://www.homestarrunner.com/&quot;&gt;Is not dead&lt;/a&gt;&lt;/li&gt;
&lt;li &lt;?php if ( is_tree('statistics') ) echo ' class=&quot;over&quot;'; ?&gt;&gt;&lt;a href=&quot;http://www.ericlightbody.com&quot;&gt;Coolest website in the universe&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.ericlightbody.com/2009/adding-multiple-sidebars-and-other-elements-to-your-wordpress-theme/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
	</channel>
</rss>
