<?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>Willerce Blog &#187; widgets</title>
	<atom:link href="http://willerce.com/post/tag/widgets/feed" rel="self" type="application/rss+xml" />
	<link>http://willerce.com</link>
	<description>Just a WordPress weblog</description>
	<lastBuildDate>Sat, 31 Jul 2010 12:53:03 +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>WordPress不同页面显示不同的侧栏.</title>
		<link>http://willerce.com/post/24</link>
		<comments>http://willerce.com/post/24#comments</comments>
		<pubDate>Tue, 17 Jun 2008 09:50:22 +0000</pubDate>
		<dc:creator>willerce</dc:creator>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[sidebar]]></category>
		<category><![CDATA[widgets]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://willerce.com/?p=43</guid>
		<description><![CDATA[侧栏（Sidebar）作为一个向访客推荐本站其它内容的重要区域,争对不同内容显示不同的Sidebar就变得尤为重要了。比如在首页的Sidebar放上Feed收藏栏目，最新评论栏目，而在日志页Sidebar放上随机文章、相关文章、最热文章。如果你是一个GG ADER，那么你就可以让首页不显示广告，日志页显示广告。 以我目前使用的这个THEME为例。它是一个三栏主题，没有使用Widgets，几乎所有的WordPress模版侧栏内容都包含在sidebar.php文件里。所有页面全部使用这个相同的Sidebar。现在我们动手，让日志页使用 Sidebar2.php。而其它页面使用Sidebar.php 注意:下文提到的所有PHP文件，都是你目前使用的主题的目录里边的，直接在后台就可以修改。步骤是:进入后台 &#62; 外观 &#62; 主题编辑器 &#62;  然后在右侧的模版文件列表里就可以选择要修改的文件,进行操作了. 我们先来做个学习小培训，看single.php里的两行PHP语言: 1 2 &#60; ?php get_sidebar&#40;&#41;; ?&#62; &#60; ?php get_footer&#40;&#41;; ?&#62; 这两行所表示的分别是调用侧栏sidebar.php和页脚footer.php文件。大家应该都可以很容易的明白它。 两种情况: ①如果你不使用Widgets，那么很简单.只要两步。 建立一个Sidebar2.php文件,你只要将sidebar.php文件里的内容全部复制到sidebar2.php里就可以了。这里有一个快捷的方法,用FTP工具将空间上的Sidebar.php文件下载到本地并重命名为Sidebar2.php，然后上传，上传目录为模版目录. 然后,将你的日志页single.php文件里的 &#60; ?php get_sidebar&#40;&#41;; ?&#62; 修改为 &#60; ?php include_once&#40;&#34;sidebar2.php&#34;&#41;; ?&#62; 现在,你可以根据自己的喜好,更改sidebar2.php里的内容。来实现更改日志页的侧栏了。 ②如果你使用Widgets,那么也挺简单.只多了两步. 先按上文的方法操作一遍。 接着打开functions.php,找到类似这样的. 1 2 3 4 5 6 7 if &#40; function_exists&#40;'register_sidebar'&#41; &#41; register_sidebar&#40;array&#40; 'before_widget' =&#62; '&#60;li [...]]]></description>
			<content:encoded><![CDATA[<p>侧栏（Sidebar）作为一个向访客推荐本站其它内容的重要区域,争对不同内容显示不同的Sidebar就变得尤为重要了。比如在首页的Sidebar放上Feed收藏栏目，最新评论栏目，而在日志页Sidebar放上随机文章、相关文章、最热文章。如果你是一个GG ADER，那么你就可以让首页不显示广告，日志页显示广告。</p>
<p>以我目前使用的这个THEME为例。它是一个三栏主题，没有使用Widgets，几乎所有的WordPress模版侧栏内容都包含在sidebar.php文件里。所有页面全部使用这个相同的Sidebar。现在我们动手，让日志页使用 Sidebar2.php。而其它页面使用Sidebar.php</p>
<p><strong>注意:下文提到的所有PHP文件，都是你目前使用的主题的目录里边的，直接在后台就可以修改。步骤是:进入后台 &gt; 外观 &gt; 主题编辑器 &gt;  然后在右侧的模版文件列表里就可以选择要修改的文件,进行操作了.</strong></p>
<p>我们先来做个学习小培训，看single.php里的两行PHP语言:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php get_sidebar<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> ?php get_footer<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></pre></td></tr></table></div>

<p>这两行所表示的分别是调用侧栏sidebar.php和页脚footer.php文件。大家应该都可以很容易的明白它。</p>
<h2><strong>两种情况:</strong></h2>
<p><strong>①如果你不使用Widgets，那么很简单.只要两步。</strong></p>
<p><strong>建立一个Sidebar2.php文件</strong>,你只要将sidebar.php文件里的内容全部复制到sidebar2.php里就可以了。这里有一个快捷的方法,用FTP工具将空间上的Sidebar.php文件下载到本地并重命名为Sidebar2.php，然后上传，上传目录为模版目录.<br />
然后,将你的日志页single.php文件里的</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php get_sidebar<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></pre></div></div>

<p>修改为</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php <span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sidebar2.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>现在,你可以根据自己的喜好,更改sidebar2.php里的内容。来实现更改日志页的侧栏了。</p>
<p><strong>②如果你使用Widgets,那么也挺简单.只多了两步.</strong></p>
<p>先按上文的方法操作一遍。</p>
<p>接着打开functions.php,找到类似这样的.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">function_exists</span><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>
   register_sidebar<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
       <span style="color: #0000ff;">'before_widget'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;li id=&quot;%1$s&quot; class=&quot;widget %2$s&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;/li&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 class=&quot;widgettitle&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;/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></pre></td></tr></table></div>

<p>上面是一段函数，定义了一个Widget，现在，我们按它的格式，再写一次，这样，就多出一个Widget出来了。为了区别开来，第二个Widget应该要有一个名字。我们这样写，在本例中，我们将它命名为<strong>sidebar2</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=***;</span> <span style="color: #0000ff;">'名字'</span></pre></div></div>

<p>整个函数定义要这样写：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"> <span style="color: #339933;">&lt;</span> ?php
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">function_exists</span><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>
   register_sidebar<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
       <span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'sidebar2'</span><span style="color: #339933;">,</span>
       <span style="color: #0000ff;">'before_widget'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;li id=&quot;%1$s&quot; class=&quot;widget %2$s&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;/li&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 class=&quot;widgettitle&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;/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></pre></td></tr></table></div>

<p>将上面些内容加到原先的Widget定义后面。<br />
现在,你需用将sidebar2.php中原来调用默认Widgets的代码改为调用我们刚刚新建的sidebar2 Widgets。找开Sidebar2.php 找到这样的代码.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"> <span style="color: #339933;">&lt;</span> ?php <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dynamic_sidebar'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> dynamic_sidebar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #b1b100;">else</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  <span style="color: #339933;">&lt;</span> ?php <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>将它修改为</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #339933;">&lt;</span> ?php <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dynamic_sidebar'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> dynamic_sidebar<span style="color: #009900;">&#40;</span>sidebar2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #b1b100;">else</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    <span style="color: #339933;">&lt;</span> ?php <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>好了,现在,你去后台的Widget设置中,会发现,在下拉列表中多出一个Widget了.它的名字为Sidebar2,现在你需要为他添加上一些东西.<br />
<strong><br />
</strong></p>
<p>如果你需要在更多的页面使用不同的sidebar.方法是相同.请举一反三,如果无法举一反三,就举个砖头往脑袋上拍吧!<br />
目前,我在我所使用的博客测试通过,欢迎大家测试.有什么问题.欢迎留言.</p>
<p>如果你的两个侧栏的内容差不多，比如首页显示友情链接，其它页不显示。<br />
MG12提供了一个比较简便的方法：<a href="http://www.neoease.com/show-different-widgets-in-wordpress-sidebar/">http://www.neoease.com/show-different-widgets-in-wordpress-sidebar/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://willerce.com/post/24/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
