Home » WordPress

升级WP旧模板,让它支持Widgets

9 01月 2010 Views: No Comment Tags:

正如大家所见,我使用的WordPress模板已经是很旧很旧的了,是以前在WordPress 1.5还是1.0系列的默认模板之上,简单修改而来的。没有选用三栏布局,因为我喜欢用稍微大一点的字体,三栏布局对我来说侧边栏东西太多,加上文章长了以后侧边栏的下面空白很大。不过旧模板自然也就没有Widgets

No Sidebars Defined You are seeing this message because the theme you are currently using isn’t widget-aware, meaning that it has no sidebars that you are able to change. For information on making your theme widget-aware, please follow these instructions.

刚开始我还能忍受,Sidebar上没有的东西俺自己手工加上,不过用上Simple Tags插件之后,感叹到了插件和Tags的强大,Relate Post很方便,可Relate Tags就是出不来,探查一番,原因应该就是我的模板不支持Widgets。所以忍不住了,模板可以不变样,但不能不升级,我就动手把它改成支持Widgets的吧。 刚才的提示中已经给出了修改指引的链接,首先把模板中的Sidebar改成ul形式的,Theme Editor -> Sidebar (sidebar.php),第一行的ul换成div即可:

<ul id="sidebar">......中间的很多内容</ul>

看了一下,对外观没有太大影响,css应该是用id来引用的,不过还是需要调整一点css:

/* 启用Widgets后Sidebar样式有些变化,在这里修正 */
#sidebar {list-style: none;}
 #sidebar li ul li:before { content: "\00BB \0020"; }
#sidebar li ul li { margin-bottom: 5px; }

然后还是修改Sidebar模板文件,加入是否有动态Sidebar的检查、如果有就调入的PHP代码:

<ul id="sidebar">
<!--p if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) :-->
......中间的很多内容
 <!--p endif;--></ul>

代码一共就3行,前面加2行,后面加一行,就是个简单的判断而已。别着急,现在还没有完成,还有最后一步。 在模板目录下,比如我的就是wp-content/themes/fwolfs-blog这个目录,创建一个新文件functions.php,内容就4行:

<!--p  if ( function_exists('register_sidebar') ) register_sidebar(); -->

好了,现在再刷新Design -> Widgets页面,就有内容了,我的旧模板现在也支持Widgets了。 同样的,Design -> Theme Editor页面中也会多一项可编辑内容: Theme Functions (functions.php) PS1: 如果要添加多个Sidebar,需要模板的支持,也就是模板中制定需要几个Sidebar,在Design中就能管理几个(register_sidebars(n),n就是Sidebar数量)。 PS2: 如果想在Widgets的Text里面内嵌php代码,比如设定只有首页才显示BlogRoll,那么需要安装PHP Code Widget插件,并使用PHP Code代码块。直接在原来的Text中写php代码的话,会原封不动的被当成php代码输出,昏死。 PS3: 一切都折腾好了,Relate Tags功能还是得手工添加php代码块到Widgets中:

 <!--p if (is_tag()) { st_related_tags(); st_remove_related_tags(); }-->

原创文章如转载,请注明:转载自Xixis Blog [ http://www.xixis.net/ ]
本文链接地址:http://www.xixis.net/archives/wp-upgrade-the-old-template-it-supports-widgets-2.html

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.