您的位置:首页»WordPress 教程, 新手入门»  WordPress 不同页面、分类调用不同侧边栏详解

WordPress 不同页面、分类调用不同侧边栏详解

前面,我们在如何在 WordPress2.7 调用不同的页眉、页脚、侧边栏对 WordPress 如何调用不同的侧边栏的方法进行了简单的介绍。今天,辐射鱼就和大家一起来探讨 WordPress 不同页面、分类调用不同侧边栏的具体实施方法。

  1. 不同页面调用不同侧边栏
  2. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    <?php
    if(is_page()) {
        // We're on a "page", which?
         if (is_page('About')) {
            // Ah, the about page!
              get_sidebar('about'); // 调用 sidebar-about.php
        } elseif (is_page('archives')) {
              get_sidebar('archives'); // 调用 sidebar-archives.php
        } elseif (is_page('contact')) {
              get_sidebar('contact'); // 调用 sidebar-contact.php
        } else {
            // if we're not any of the above pages
              get_sidebar(); // 调用 sidebar.php
    } else {
        get_sidebar(); // 调用 sidebar.php
    }
    ?>

    或者

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
    <?php 
        //to be able to use this outside the_Loop 
        if ( have_posts() ) { the_post(); rewind_posts(); } 
     
        if ( is_home() || is_front_page() ) { 
            get_sidebar('home'); 
            //gets sidebar-home.php 
        } elseif ( is_archive() ) { 
            get_sidebar('archive'); 
            //gets sidebar-archive.php 
        } elseif ( is_page() ) { 
            get_sidebar('page'); 
            //gets sidebar-page.php 
        } elseif ( is_single() ) { 
            get_sidebar('single'); 
            //gets sidebar-single.php 
        } else { 
            get_sidebar() 
            //gets sidebar.php 
        } 
    ?>
  3. 不同分类调用不同侧边栏
  4. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
    <?php
    //to be able to use this outside the loop
    if( have_posts() ) { the_post(); rewind_posts(); } 
     
    if( in_category('1') ) {
         get_sidebar('themes');
        //调用 sidebar-themes.php
    } elseif ( in_category('2') ) {
         get_sidebar('plugins');
        //调用 sidebar-plugins.php
    } elseif ( in_category('3') ) {
         get_sidebar('course');
        //调用 sidebar-course.php
    } elseif ( in_category('4') || in_category('5') || in_category('6') ) {
         get_sidebar('catRest');
        //调用 sidebar-catRest.php
    } else {
         get_sidebar()
        //调用 sidebar.php
    }
    ?>

怎么样,非常简单吧?!如果你有什么 WordPress 小技巧,欢迎和大家一起分享。

9个评论

  • Reply 1 雪深

    April 9th, 2009 at 16:16

    沙发么

  • Reply 2 雪深

    April 9th, 2009 at 16:16

    果然沙发 走人

  • Reply 3 welee

    April 10th, 2009 at 15:24

    这个好,最近在搞侧边栏,来得合时!

  • Reply 4 wuai

    April 10th, 2009 at 17:03

    站长您好!

    按照你的文章,我想把2个page调用sidebar_single.php侧边栏,我只是在page.php的最后两行做了修改,结果还是不行,不知道我的代码是否设置错误?
    —————————

    ——————————
    我在single.php里面已经成功地调用了sidebar_single.php,采用的是
    但是在page.php里面,因为增加了页面条件判断,所以没搞掂。不知您有无高见?

    • Reply 5 辐射鱼

      April 12th, 2009 at 09:44

      按照第一段代码按图索骥,没问题的,你再仔细检查一下

  • Reply 6 leesum

    April 12th, 2009 at 12:16

    太及时了,最近正琢磨这种方法呢

  • Reply 7 庄子

    August 13th, 2009 at 18:27

    把这段代码加的那里啊???

    不懂啊

    • Reply 8 辐射鱼

      August 14th, 2009 at 21:04

      single.php之类的地方

  • Reply 9 微扬

    February 22nd, 2010 at 22:09

    终于找到了