让WordPress 友情链接只显示在首页

很多时候我们只希望将友情链接显示在博客的首页而没有必要显示在所有的文章页面,就比如我的博客,首页放置有30多个友情链接链接,如果在文章页面也显示这么多链接的话,我会觉得页面很不美观,而且在文章页面的Sidebar里我更乐意放置更为重要的导航信息,比如最新文章,热门文章,标签等。

对于WordPress用户而言,只要通过修改模板里面的sidebar.php就可以很方便的控制sidebar的显示信息,比如这样添加:

  1. <?php /* If this is the frontpage */ if ( is_home() ) { ?>
  2. <h3>Links</h3>
  3. <ul>
  4. <?php get_links('-1', '<li>', '</li>', '<br />', FALSE, 'id', FALSE, FALSE, -1, FALSE); ?>
  5. </ul>
  6. <?php } ?>

就可以让友情链接只显示在首页(home)而不显示在文章页面(single),同理,如果我要将tags只显示在文章页面而不显示在博客首页的话,可以这样在sidebar这样添加:

  1. <?php /* If this is the frontpage */ if ( is_single() ) { ?>
  2. <h3>Tags</h3>
  3. <?php wp_tag_cloud('smallest=10&largest=15'); ?>
  4. </ul>
  5. <?php } ?>

is_home()表示首页,is_single()表示文章页面,而is_page()则表示单个页面。再比如,想要将友情链接只显示在首页和独立页面,则可以这样在sidebar添加:

  1. <?php /* If this is the frontpage */ if ( is_home() || is_page() ) { ?>
  2. <h3>Links</h3>
  3. <ul>
  4. <?php get_links('-1', '<li>', '</li>', '<br />', FALSE, 'id', FALSE, FALSE, -1, FALSE); ?>
  5. </ul>
  6. <?php } ?>

if ( is_home() || is_page() )就是判断语句,表示“如果在首页或者在单个页面,则执行某某操作”

现在,你应该明白该怎么调用了吧:-)

Posted in WordPress. Tags: .

1 Comment

  • At 2008.05.15 11:11, 小一 said:

    WP有管理友情链接的功能吧?
    我还在停留在手动添加阶段,呵呵!

    (Required)
    (Required, will not be published)