WordPress不同页面显示不同的侧栏.

侧栏(Sidebar)作为一个向访客推荐本站其它内容的重要区域,争对不同内容显示不同的Sidebar就变得尤为重要了。比如在首页的Sidebar放上Feed收藏栏目,最新评论栏目,而在日志页Sidebar放上随机文章、相关文章、最热文章。如果你是一个GG ADER,那么你就可以让首页不显示广告,日志页显示广告。

以我目前使用的这个THEME为例。它是一个三栏主题,没有使用Widgets,几乎所有的Wordpress模版侧栏内容都包含在sidebar.php文件里。所有页面全部使用这个相同的Sidebar。现在我们动手,让日志页使用 Sidebar2.php。而其它页面使用Sidebar.php

注意:下文提到的所有PHP文件,都是你目前使用的主题的目录里边的,直接在后台就可以修改。步骤是:进入后台 > 外观 > 主题编辑器 >  然后在右侧的模版文件列表里就可以选择要修改的文件,进行操作了.

我们先来做个学习小培训,看single.php里的两行PHP语言:

1
2
< ?php get_sidebar(); ?>
< ?php get_footer(); ?>

这两行所表示的分别是调用侧栏sidebar.php和页脚footer.php文件。大家应该都可以很容易的明白它。

两种情况:

①如果你不使用Widgets,那么很简单.只要两步。

建立一个Sidebar2.php文件,你只要将sidebar.php文件里的内容全部复制到sidebar2.php里就可以了。这里有一个快捷的方法,用FTP工具将空间上的Sidebar.php文件下载到本地并重命名为Sidebar2.php,然后上传,上传目录为模版目录.
然后,将你的日志页single.php文件里的

< ?php get_sidebar(); ?>

修改为

< ?php include_once("sidebar2.php"); ?>

现在,你可以根据自己的喜好,更改sidebar2.php里的内容。来实现更改日志页的侧栏了。

②如果你使用Widgets,那么也挺简单.只多了两步.

先按上文的方法操作一遍。

接着打开functions.php,找到类似这样的.

1
2
3
4
5
6
7
 if ( function_exists('register_sidebar') )
   register_sidebar(array(
       'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
       'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',
     ));

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

'name' =***; '名字'

整个函数定义要这样写:

1
2
3
4
5
6
7
8
9
 < ?php
 if ( function_exists('register_sidebar') )
   register_sidebar(array(
       'name' => 'sidebar2',
       'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
       'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',
     ));

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

1
2
 < ?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar() ) : else : ?>
  < ?php endif; ?>

将它修改为

1
2
    < ?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar(sidebar2) ) : else : ?>
    < ?php endif; ?>

好了,现在,你去后台的Widget设置中,会发现,在下拉列表中多出一个Widget了.它的名字为Sidebar2,现在你需要为他添加上一些东西.

如果你需要在更多的页面使用不同的sidebar.方法是相同.请举一反三,如果无法举一反三,就举个砖头往脑袋上拍吧!
目前,我在我所使用的博客测试通过,欢迎大家测试.有什么问题.欢迎留言.

如果你的两个侧栏的内容差不多,比如首页显示友情链接,其它页不显示。
MG12提供了一个比较简便的方法:http://www.neoease.com/show-different-widgets-in-wordpress-sidebar/

This entry was posted in Wordpress and tagged , , . Bookmark the permalink.

10 Responses to WordPress不同页面显示不同的侧栏.

  1. Pingback: Wopus测试站 » WordPress不同页面调用不同侧边栏技术详解

  2. patrick says:

    版主你好,我用的是mimbo主题,在sidebar2.php中找不到您例子提到的那些语句,下面是该主题中相关内容,不知如何修改,谢谢!

    <?php

    // grab the contents of the sidebar
    ob_start();
    dynamic_sidebar()
    $sidebarContent = ob_get_contents();
    ob_end_clean();

    if ( $sidebarContent != “” ) {

    echo “”;
    echo $sidebarContent;
    echo “”;

    } else {

    ?>

  3. willerce says:

    @patrick 不知道你想采用什么方法显示不同的侧栏.是文中的情况1,还是情况2 ?

  4. sundyme says:

    链接已改正。不好意思,上次疏忽没有找到于是连接。

  5. Donald says:

    这篇文章非常有帮助,谢谢啦…其实我要的很简单,就是首页显示友情链接,其他页不显示,嘿嘿…

  6. ZH CEXO says:

    搜索而来,挺好的~

  7. shenholmes says:

    想请问,如果我新的页面不想要侧栏,应该怎么做?
    谢谢~~

  8. willerce says:

    @shenholmes 找到页面中的< ?php get_sidebar(); ?>删除。

  9. ohyear says:

    恩,就是想找这个。但在这段
    ‘sidebar2′,。。。。。。
    我是去掉“< ?php”,才能正常使用。我是刚学用WordPress的菜鸟,是学到东西了。

Leave a Reply

Your email address will not be published. Required fields are marked *

*

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