网络营销电子商务研究中心

网络营销电子商务研究中心 (https://www.0058.net/index.php)
-   WordPress (https://www.0058.net/forumdisplay.php?f=68)
-   -   WordPress不同分类使用不同的侧边栏sidebar (https://www.0058.net/showthread.php?t=4380)

topvip 2012-07-05 10:25 PM

WordPress不同分类使用不同的侧边栏sidebar
 
WordPress群里有朋友问到如何使不同的页面调用不同的侧边栏,比如首页的侧边栏和分类页的侧边栏不一样,或者分类页的侧边栏和内容页的又不一样,这样的功能我们可以用代码实现,前提就是你的侧边栏不是通过管理控制板后台里面的小工具来生成的,而是在模板文件sidebar.php中代码生成的。只有是代码生成的侧边栏,才能同样用代码来控制不同页面显示不同的侧边栏内容。

下面的代码是我的sidebar.php模板文件里面的部分代码:
Code:

<li><h2>标签云图</h2>
  <div>
  <?php wp_tag_cloud('smallest=8&largest=20&orderby=count&order=DESC&number=30'); ?>
  </div>
 </li>

Code:

<li><h2>最新文章</h2>
  <?php query_posts('showposts=7&cat=-111'); ?> 
  <ul> 
  <?php while (have_posts()) : the_post(); ?> 
  <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li> 
  <?php endwhile;?> 
  </ul>
 </li>

假如我们只想让标签云图在首页显示,不在分类栏目下面显示出来,那么我们可以在标签云图的代码前面加上条件控制代码,is_home(),最后的代码如下:
Code:

<?php if(is_home()): ?>
 <li><h2>标签云图</h2>
  <div>
  <?php wp_tag_cloud('smallest=8&largest=20&orderby=count&order=DESC&number=30'); ?>
  </div>
 </li>
<?php endif; ?>

Code:

<li><h2>最新文章</h2>
  <?php query_posts('showposts=7&cat=-111'); ?> 
  <ul> 
  <?php while (have_posts()) : the_post(); ?> 
  <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li> 
  <?php endwhile;?> 
  </ul>
 </li>

如首页的侧边栏里面的最新文章调用所有分类的最新文章,而某个分类页面的侧边栏里面的最新文章显示该分类里面的最新文章。这种情况,我们可以通过加入下面的代码来实现:
Code:

<?php if (in_category('111')) : ?>


All times are GMT +8. The time now is 10:38 PM.

Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.