网络营销电子商务研究中心  
How to buy the best prescription safety glasses in Canada? Let's study!
Go Back   网络营销电子商务研究中心 > 网站建设 > WordPress
User Name
Password
 
FAQ Members List Calendar Cheap Glasses

Reply
 
Thread Tools Display Modes
  #1   IP: 114.238.4.59
Old 2010-08-30, 09:20 PM
topvip topvip is offline
超级版主
 
Join Date: 2006-01-04
Posts: 1206
topvip 正向着好的方向发展
Default WordPress 不同页面显示不一样的侧边栏

本文将告诉你如何在 WordPress 不同的页面显示不同的侧边栏内容. 如: 可以在首页显示 Archives, 但要在其他页面显示 Categories. 为什么要这么做呢? 因为访客在不同的页面会对不一样的内容感兴趣, 在首页可能他们愿意看到目录和索引等内容, 而在单篇文章则会对文章相关内容更加关注. 所以我们可以通过不同的侧边栏 Widgets 来引导访客.

这是个极其简单的主题加工, 本不想多言, 但貌似有些朋友还是不太了解, 所以专门发个文章说说, 老鸟请绕行.


首先要说明的是, 以下操作目前只能通过修改 sidebar.php 或相关代码达到目的. 目前还没发现有这样的插件, 我不知道开发这样一个针对 Widget 的插件是否有价值, 但有兴趣的朋友可以一试.

Okay, 下面我们来个例子吧, 假如现在的 Widget 结构如下:
Code:
<div class="widget xxx_widget">
	<h3>Widget Title</h3>
	<ul>
		<li>Item 1</li>
		<li>Item 2</li>
		<li>Item 3</li>
	</ul>
</div>
如果我们要在首页显示 Archives, 单篇文章显示 Categories, 其他页面显示 Meta, 则需要改动代码如下:
Code:
<?php if (is_home()) : ?>
	<div class="widget archives_widget">
		<h3>Archives</h3>
		<ul>
			<?php wp_get_archives(); ?>
		</ul>
	</div>
<?php else if (is_single()) : ?>
	<div class="widget categories_widget">
		<h3>Categories</h3>
		<ul>
			<?php wp_list_cats(); ?>
		</ul>
	</div>
<?php else : ?>
	<div class="widget meta_widget">
		<h3>Meta</h3>
		<ul>
			<?php wp_register(); ?>
			<li><?php wp_loginout(); ?></li>
		</ul>
	</div>
<?php endif; ?>
不知道你是否已经注意到, 其实关键在 is_home() 和 is_single() 方法的调用, 通过 if 语句限制显示条件. 你可以将它们换成其他限制条件, 以达到不页面的不同地方显示不同内容的目的, 而不仅仅是侧边栏. 还可以参考一下我发布过的那些主题里 archive.php 文件里 Archive 页面的标题是怎么实现的, 只要这个你弄懂了, 其他的就小菜一碟了.
WordPress 已经为我们提供了足够多的页面筛选方法, 以下一些相关资料:
Codex 关于页面限制方法的说明:
http://codex.wordpress.org/Conditional_Tags
你也可以打开 wp-includes/query.php 查看 WordPress 的源代码.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Install WordPress on IIS topvip Blog(博客)/Wiki(维客)/RSS/Cms 1 2010-01-25 08:31 PM
让wordpress首页显示特殊页面(flash首页等) yahoo Blog(博客)/Wiki(维客)/RSS/Cms 0 2009-02-25 06:58 AM
备份wordpress数据库的七种方法 car WordPress 0 2008-11-15 11:12 PM
wordpress 搜索引擎优化的二十条实用技巧 car 搜索引擎优化 0 2008-03-11 08:33 AM
WordPress 中文包 sunshine Blog(博客)/Wiki(维客)/RSS/Cms 0 2007-03-12 10:16 AM

Prescription-glasses.com offers prescription glasses online at discount prices.
All times are GMT +8. The time now is 10:54 AM.


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