<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>站趣-分享建站的乐趣主题开发</title>
	<atom:link href="http://eachsite.org/category/wordpress-themes/themes-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://eachsite.org</link>
	<description>站趣致力于一起分享建站的乐趣，并制作各种 WordPress 使用教程、主题以及插件，并关注各种互联网新鲜应用，使国人轻松建站、快乐建站。</description>
	<lastBuildDate>Tue, 27 Dec 2011 05:29:07 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>高亮 WordPress 搜索结果 JQuery 篇</title>
		<link>http://eachsite.org/highlight-keywords-via-jquery/</link>
		<comments>http://eachsite.org/highlight-keywords-via-jquery/#comments</comments>
		<pubDate>Sun, 12 Apr 2009 01:24:43 +0000</pubDate>
		<dc:creator>辐射鱼</dc:creator>
				<category><![CDATA[WordPress 教程]]></category>
		<category><![CDATA[主题开发]]></category>
		<category><![CDATA[进阶使用]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://eachsite.org/?p=481</guid>
		<description><![CDATA[我曾在站趣-分享建站的乐趣提过如何高亮 WordPress 搜索关键词，可这个方法有一个缺陷，那就是只有当文章标题里有搜索关键词时才会高亮，换句话说，如果关键词出现在正文中，就并不会高亮显示。那么，有什么方法能在文章标题和正文高亮显示搜索关键词呢？当然有，那就是 JQuery。


首先，编辑主题 functions.php 文件，加入以下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function wps_set_query&#40;&#41; &#123;
  $query  = attribute_escape&#40;get_search_query&#40;&#41;&#41;;
&#160;
  if&#40;strlen&#40;$query&#41; &#62; 0&#41;&#123;
    echo '
      &#60;script type=&#34;text/javascript&#34;&#62;
        var wps_query  = &#34;'.$query.'&#34;;
      &#60;/script&#62;
    ';
  &#125;
&#125;
&#160;
function wps_init_jquery&#40;&#41; &#123;
 [...]]]></description>
			<content:encoded><![CDATA[<p>我曾在站趣-分享建站的乐趣提过如何<a href="http://eachsite.org/enlight-keywords-in-wp-search/">高亮 WordPress 搜索关键词</a>，可这个方法有一个缺陷，那就是只有当文章标题里有搜索关键词时才会高亮，换句话说，如果关键词出现在正文中，就并不会高亮显示。那么，有什么方法能在文章标题和正文高亮显示搜索关键词呢？当然有，那就是 JQuery。<br />
<img src="http://eachsite.org/wp-content/eachsite/2009/04/search_result-300x170.gif" alt="高亮 WordPress 搜索结果" title="高亮 WordPress 搜索结果" width="300" height="170" class="aligncenter size-medium wp-image-489" /><span id="more-481"></span></p>
<ol>
<li>首先，编辑主题 functions.php 文件，加入以下代码</li>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> wps_set_query<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$query</span>  <span style="color: #339933;">=</span> attribute_escape<span style="color: #009900;">&#40;</span>get_search_query<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'
      &lt;script type=&quot;text/javascript&quot;&gt;
        var wps_query  = &quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$query</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;;
      &lt;/script&gt;
    '</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> wps_init_jquery<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  wp_enqueue_script<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'jquery'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'init'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'wps_init_jquery'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_print_scripts'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'wps_set_query'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<li>编辑主题 header.php 文件，加入以下代码：</li>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>style type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/css&quot;</span> media<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;screen&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">.</span>wps <span style="color: #009900;">&#123;</span> background<span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">#D3E18A; }
</span>  <span style="color: #339933;">&lt;/</span>style<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
  jQuery<span style="color: #339933;">.</span>fn<span style="color: #339933;">.</span>extend<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    highlight<span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>search<span style="color: #339933;">,</span> insensitive<span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">var</span> regex <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RegExp<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;(&lt;[^&gt;]*&gt;)|(<span style="color: #000099; font-weight: bold;">\\</span>b&quot;</span><span style="color: #339933;">+</span> search<span style="color: #339933;">.</span>replace<span style="color: #009900;">&#40;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">-.*+</span>?^$<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span>\<span style="color: #009900;">&#93;</span>\<span style="color: #339933;">/</span>\\<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span>g<span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\\</span><span style="color: #006699; font-weight: bold;">$1</span>&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">,</span> insensitive ? <span style="color: #0000ff;">&quot;ig&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;g&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">return</span> this<span style="color: #339933;">.</span>html<span style="color: #009900;">&#40;</span>this<span style="color: #339933;">.</span>html<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>replace<span style="color: #009900;">&#40;</span>regex<span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span> b<span style="color: #339933;">,</span> c<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>a<span style="color: #339933;">.</span>charAt<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;&lt;&quot;</span><span style="color: #009900;">&#41;</span> ? a <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;&lt;strong class=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span> <span style="color: #339933;">+</span> c <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;&lt;/strong&gt;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>ready<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#post-area&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>highlight<span style="color: #009900;">&#40;</span>wps_query<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;wps&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></pre></td></tr></table></div>

<p>代码注释：<br />
①  你可将 css 写入 style.css 中；<br />
②  需将 <strong>post-area</strong> 修改为你所用主题的搜索结果所在 div；<br />
③  该段代码应放在 <strong></head></strong> 标签之前。
</ol>
<p>如何，本篇 <a href="http://eachsite.org/tag/course/">WordPress 教程</a>不复杂吧？！</p>
<ul class="related_post"><li><a href="http://eachsite.org/transfer-wordpress-via-ssh/" title="通过SSH迁移WordPress博客">通过SSH迁移WordPress博客</a> (13)</li><li><a href="http://eachsite.org/a-to-z-of-wordpress-htaccess-hacks/" title="从A到Z 26条 WordPress .htaccess 技巧">从A到Z 26条 WordPress .htaccess 技巧</a> (13)</li><li><a href="http://eachsite.org/how-to-create-an-iphone-version-of-your-blog/" title="如何使你的博客支持 iPhone">如何使你的博客支持 iPhone</a> (8)</li><li><a href="http://eachsite.org/how-to-use-menu-in-wp-post/" title="如何为 WordPress 文章创建导航目录">如何为 WordPress 文章创建导航目录</a> (14)</li><li><a href="http://eachsite.org/auto-insert-content-after-each-post/" title="自动为 WordPress 文章、Feed 添加版权等信息">自动为 WordPress 文章、Feed 添加版权等信息</a> (12)</li><li><a href="http://eachsite.org/get-latest-sticky-posts-in-wp/" title="如何获取 WordPress 最新置顶文章列表">如何获取 WordPress 最新置顶文章列表</a> (12)</li><li><a href="http://eachsite.org/how-to-get-a-particular-sidebar-in-wordpress/" title="WordPress 不同页面、分类调用不同侧边栏详解">WordPress 不同页面、分类调用不同侧边栏详解</a> (9)</li><li><a href="http://eachsite.org/wp-page-menu/" title="wp_page_menu 函数使用详解">wp_page_menu 函数使用详解</a> (3)</li><li><a href="http://eachsite.org/use-getavatar-to-add-gravatar-in-wp/" title="使用 get_avatar 为 WP 自定义 Gravatar 默认头像">使用 get_avatar 为 WP 自定义 Gravatar 默认头像</a> (22)</li><li><a href="http://eachsite.org/add-a-default-gravatar-in-wordpress/" title="如何在 WordPress 中添加 Gravatar 默认头像">如何在 WordPress 中添加 Gravatar 默认头像</a> (11)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://eachsite.org/highlight-keywords-via-jquery/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>使用 get_avatar 为 WP 自定义 Gravatar 默认头像</title>
		<link>http://eachsite.org/use-getavatar-to-add-gravatar-in-wp/</link>
		<comments>http://eachsite.org/use-getavatar-to-add-gravatar-in-wp/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 15:09:45 +0000</pubDate>
		<dc:creator>辐射鱼</dc:creator>
				<category><![CDATA[WordPress 教程]]></category>
		<category><![CDATA[主题开发]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[主题]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://eachsite.org/?p=421</guid>
		<description><![CDATA[在上一篇文章中我们谈到如何在 WordPress 中添加 Gravatar 默认头像，经雪深提醒，我翻了翻 codex，发现还可以用 get_avatar 函数指定默认 Gravatar 头像。
get_avatar 是 WordPress2.5 新增的一个函数，所以此方法只适用于 WordPress2.5 及以上版本，其基本用法如下：

1
2
3
&#60;?php 
   echo get_avatar&#40; $id_or_email, $size = '96', $default = '&#60;path_to_url&#62;' &#41;; 
?&#62;

id_or_email：必备参数，可以是一个 User ID（字符串），一个 email 地址，或是一个评论对象。Note:在绝大多数 WordPress 主题中，我们可以将 id_or_email 赋值为 $comment 或 get_comment_author_email()，用以显示评论者 Gravatar 头像，另外，我们也可以使用 get_the_author_id() 显示文章作者 Gravatar 头像。
size：可选参数，定义 Gravatar 头像大小，最大尺寸为512px，默认为96&#215;96
default：可选参数，用以自定义 Gravatar 默认头像地址
如果您还在使用 WordPress2.5 以下版本，那么得做如下 hack：

1
2
3
4
5
6
7
8
   [...]]]></description>
			<content:encoded><![CDATA[<p>在上一篇文章中我们谈到<a href="http://eachsite.org/add-a-default-gravatar-in-wordpress/">如何在 WordPress 中添加 Gravatar 默认头像</a>，经雪深提醒，我翻了翻 <a href="http://codex.wordpress.org/Using_Gravatars">codex</a>，发现还可以用 <strong>get_avatar</strong> 函数指定默认 Gravatar 头像。<br />
<strong>get_avatar</strong> 是 WordPress2.5 新增的一个函数，所以此方法只适用于 WordPress2.5 及以上版本，其基本用法如下：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
   <span style="color: #b1b100;">echo</span> get_avatar<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$id_or_email</span><span style="color: #339933;">,</span> <span style="color: #000088;">$size</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'96'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$default</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;path_to_url&gt;'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>id_or_email</strong>：必备参数，可以是一个 User ID（字符串），一个 email 地址，或是一个评论对象。<em>Note:在绝大多数<span id="more-421"></span> WordPress 主题中，我们可以将 <strong>id_or_email</strong> 赋值为 <strong>$comment</strong> 或 get_comment_author_email()，用以显示评论者 Gravatar 头像，另外，我们也可以使用 <strong>get_the_author_id()</strong> 显示文章作者 Gravatar 头像。</em><br />
<strong>size</strong>：可选参数，定义 Gravatar 头像大小，最大尺寸为512px，默认为96&#215;96<br />
<strong>default</strong>：可选参数，用以自定义 Gravatar 默认头像地址<br />
如果您还在使用 WordPress2.5 以下版本，那么得做如下 hack：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'get_avatar'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> get_avatar<span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//alternate gravatar code for &lt; 2.5</span>
      <span style="color: #000088;">$grav_url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://www.gravatar.com/avatar.php?gravatar_id=
         &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&amp;default=&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$default</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&amp;size=&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$size</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;img src='<span style="color: #006699; font-weight: bold;">$grav_url</span>'/&gt;&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>好了，使用 get_avatar 函数为 <a href="http://eachsite.org/tag/wordpress/">WordPress</a> 自定义 Gravatar 默认头像就介绍到这里。正所谓条条道路通罗马，您喜欢哪个方法呢？如果您有任何关于 <a href="http://eachsite.org/tag/course/">WordPress 的小技巧</a>，欢迎和站趣一起分享。</p>
<ul class="related_post"><li><a href="http://eachsite.org/how-to-create-an-iphone-version-of-your-blog/" title="如何使你的博客支持 iPhone">如何使你的博客支持 iPhone</a> (8)</li><li><a href="http://eachsite.org/add-a-default-gravatar-in-wordpress/" title="如何在 WordPress 中添加 Gravatar 默认头像">如何在 WordPress 中添加 Gravatar 默认头像</a> (11)</li><li><a href="http://eachsite.org/show-wordpress-category-images/" title="WordPress CMS 主题技巧：显示 WordPress 分类图片">WordPress CMS 主题技巧：显示 WordPress 分类图片</a> (9)</li><li><a href="http://eachsite.org/wordpress-cms-tips-latest-posts-by-cat/" title="WordPress CMS 技巧：按照分类调用最新文章">WordPress CMS 技巧：按照分类调用最新文章</a> (26)</li><li><a href="http://eachsite.org/wordpress-shortcodes/" title="精通 WordPress 短代码">精通 WordPress 短代码</a> (6)</li><li><a href="http://eachsite.org/wordpress-post-images-tips/" title="自动获取 WordPress 缩略图增强应用">自动获取 WordPress 缩略图增强应用</a> (5)</li><li><a href="http://eachsite.org/get-1st-images-in-wordpress/" title="自动获取 WordPress 缩略图">自动获取 WordPress 缩略图</a> (12)</li><li><a href="http://eachsite.org/wordpress27-threading-comments/" title="WordPress2.7 嵌套评论">WordPress2.7 嵌套评论</a> (7)</li><li><a href="http://eachsite.org/enhance-theme-suit-wordpress27/" title="如何使你的主题兼容 WordPress2.7">如何使你的主题兼容 WordPress2.7</a> (9)</li><li><a href="http://eachsite.org/safest-way-to-include-plugin-code-in-themes/" title="在 WordPress 主题中安全的调用插件代码">在 WordPress 主题中安全的调用插件代码</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://eachsite.org/use-getavatar-to-add-gravatar-in-wp/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>如何在 WordPress 中添加 Gravatar 默认头像</title>
		<link>http://eachsite.org/add-a-default-gravatar-in-wordpress/</link>
		<comments>http://eachsite.org/add-a-default-gravatar-in-wordpress/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 16:10:21 +0000</pubDate>
		<dc:creator>辐射鱼</dc:creator>
				<category><![CDATA[WordPress 教程]]></category>
		<category><![CDATA[主题开发]]></category>
		<category><![CDATA[进阶使用]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[主题]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://eachsite.org/?p=417</guid>
		<description><![CDATA[我们知道 WordPress 会自动匹配评论者/留言者的 email 并据此显示其 Gravatar 头像，但对于仍未使用 Gravatar 头像的评论者/留言者，便只显示丑丑的默认头像。你是不是和辐射鱼一样，早看这几个丑丑的默认头像不顺眼了呢？别担心，强大的 WordPress 已经为我们考虑到了这个问题，我们只需用 WordPress filter action 替换 avatar_defaults，即可新增一个图片作为默认头像，效果图如下：

add_filter() 过滤器函数使用释疑：
我们使用 WordPress 函数时，可以通过 add_filter() 过滤器钩子添加你的新过滤器。使用方法：

add_filter&#40;’hook_name’, ‘your_filter’, &#91;priority&#93;, &#91;accepted_args&#93;&#41;;

hook_name：WordPress 提供的过滤器钩子，即你要 hack 的函数；
your_filter：你为 hook_name 定义的新函数名，它可以是一个 php 自带函数,一个 WordPress 核心函数,或是一个你的自定义函数；
priority：这是一个整型的可选参数，它用来指定为一个过滤器钩子应用过滤器的顺序，其默认值为10，一个拥有优先级为 1 的过滤器将会比拥有优先级为 2 的先被应用，数字越小，其优先级将越高；
accepted_args：这也是一个整型的可选参数，用以定义你的函数能接收多少个参数，默认值为 1。
下面，开始我们的小手术吧！！！
在主题 functions.php 文件中自定义 Gravatar 默认头像：

1
2
3
4
5
6
7
8
// add default gravatar
add_filter&#40; 'avatar_defaults', 'newgravatar' &#41;;
&#160;
function newgravatar &#40;$avatar_defaults&#41; &#123;
    $myavatar [...]]]></description>
			<content:encoded><![CDATA[<p>我们知道 <a href="http://eachsite.org/tag/wordpress/">WordPress</a> 会自动匹配评论者/留言者的 email 并据此显示其 <a href="http://www.gravatar.com/">Gravatar</a> 头像，但对于仍未使用 Gravatar 头像的评论者/留言者，便只显示丑丑的默认头像。你是不是和辐射鱼一样，早看这几个丑丑的默认头像不顺眼了呢？别担心，强大的 WordPress 已经为我们考虑到了这个问题，我们只需用 WordPress filter action 替换 <strong>avatar_defaults</strong>，即可新增一个图片作为默认头像，效果图如下：<br />
<img src="http://eachsite.org/wp-content/eachsite/2009/03/add-default-gravatar-300x202.gif" alt="在 WordPress 中添加默认 Gravatar 头像" title="在 WordPress 中添加默认 Gravatar 头像" width="300" height="202" class="aligncenter size-medium wp-image-418" /><br />
<strong>add_filter() 过滤器函数使用释疑</strong>：<br />
我们使用 WordPress 函数时，可以通过 <strong>add_filter()</strong> 过滤器钩子添加你的新过滤器。使用方法：<span id="more-417"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">add_filter<span style="color: #009900;">&#40;</span>’hook_name’<span style="color: #339933;">,</span> ‘your_filter’<span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span>priority<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span>accepted_args<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>hook_name</strong>：WordPress 提供的过滤器钩子，即你要 hack 的函数；<br />
<strong>your_filter</strong>：你为 hook_name 定义的新函数名，它可以是一个 php 自带函数,一个 WordPress 核心函数,或是一个你的自定义函数；<br />
<strong>priority</strong>：这是一个整型的可选参数，它用来指定为一个过滤器钩子应用过滤器的顺序，其默认值为10，一个拥有优先级为 1 的过滤器将会比拥有优先级为 2 的先被应用，数字越小，其优先级将越高；<br />
<strong>accepted_args</strong>：这也是一个整型的可选参数，用以定义你的函数能接收多少个参数，默认值为 1。<br />
下面，开始我们的小手术吧！！！<br />
<strong>在主题 functions.php 文件中自定义 Gravatar 默认头像</strong>：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// add default gravatar</span>
add_filter<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'avatar_defaults'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'newgravatar'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> newgravatar <span style="color: #009900;">&#40;</span><span style="color: #000088;">$avatar_defaults</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$myavatar</span> <span style="color: #339933;">=</span> get_bloginfo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'template_directory'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/images/boy.jpg'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$avatar_defaults</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$myavatar</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;eachSite&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$avatar_defaults</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>使用方法</strong>：</p>
<ol>
<li>复制以上代码到主题 functions.php 文件中</li>
<li>上传 boy.jpg 文件到 wp-content/themes/yourusingtheme/images/ 目录</li>
</ol>
<p><strong>update:经雪深提醒，新增<a href="http://eachsite.org/use-getavatar-to-add-gravatar-in-wp/">使用 get_avatar 为 WP 自定义 Gravatar 默认头像</a></strong><br />
另外：使用 WordPress2.7 的同学，最好使用 add_filter() 为 WordPress 添加 Gravatar 默认头像！！！<br />
怎么样，对这篇 <a href="http://eachsite.org/tag/course/">WordPress 教程</a>感兴趣吧？</p>
<ul class="related_post"><li><a href="http://eachsite.org/how-to-create-an-iphone-version-of-your-blog/" title="如何使你的博客支持 iPhone">如何使你的博客支持 iPhone</a> (8)</li><li><a href="http://eachsite.org/use-getavatar-to-add-gravatar-in-wp/" title="使用 get_avatar 为 WP 自定义 Gravatar 默认头像">使用 get_avatar 为 WP 自定义 Gravatar 默认头像</a> (22)</li><li><a href="http://eachsite.org/show-wordpress-category-images/" title="WordPress CMS 主题技巧：显示 WordPress 分类图片">WordPress CMS 主题技巧：显示 WordPress 分类图片</a> (9)</li><li><a href="http://eachsite.org/wordpress-cms-tips-latest-posts-by-cat/" title="WordPress CMS 技巧：按照分类调用最新文章">WordPress CMS 技巧：按照分类调用最新文章</a> (26)</li><li><a href="http://eachsite.org/wordpress-shortcodes/" title="精通 WordPress 短代码">精通 WordPress 短代码</a> (6)</li><li><a href="http://eachsite.org/wordpress-post-images-tips/" title="自动获取 WordPress 缩略图增强应用">自动获取 WordPress 缩略图增强应用</a> (5)</li><li><a href="http://eachsite.org/get-1st-images-in-wordpress/" title="自动获取 WordPress 缩略图">自动获取 WordPress 缩略图</a> (12)</li><li><a href="http://eachsite.org/wordpress27-threading-comments/" title="WordPress2.7 嵌套评论">WordPress2.7 嵌套评论</a> (7)</li><li><a href="http://eachsite.org/enhance-theme-suit-wordpress27/" title="如何使你的主题兼容 WordPress2.7">如何使你的主题兼容 WordPress2.7</a> (9)</li><li><a href="http://eachsite.org/safest-way-to-include-plugin-code-in-themes/" title="在 WordPress 主题中安全的调用插件代码">在 WordPress 主题中安全的调用插件代码</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://eachsite.org/add-a-default-gravatar-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>WordPress CMS 主题技巧：显示 WordPress 分类图片</title>
		<link>http://eachsite.org/show-wordpress-category-images/</link>
		<comments>http://eachsite.org/show-wordpress-category-images/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 16:21:46 +0000</pubDate>
		<dc:creator>辐射鱼</dc:creator>
				<category><![CDATA[WordPress 教程]]></category>
		<category><![CDATA[主题开发]]></category>
		<category><![CDATA[进阶使用]]></category>
		<category><![CDATA[cms]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[主题]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://eachsite.org/?p=379</guid>
		<description><![CDATA[在看了上一篇 WordPress CMS 技巧：按照分类调用最新文章之后，你是不是觉得能为分类添加图片就更完美呢？
我们知道，WordPress 暂时还不支持为分类添加图片，但这难不倒我们众多的 WordPress 爱好者，我们稍微 DIY  一下 WordPress 主题，一个山寨 WordPress 分类图标就出来了。
思路：我们知道，get the category 可以获取 cat_ID（分类 ID）、cat_name（分类名）、category_nicename（分类缩略名）、category_description（分类描述）、category_parent（分类层级） 以及 category_count 等信息，那么，我们就可以用 category_nicename 命名图片，然后匹配并输出。可能有点拗口，看下代码吧，很简单：

1
2
3
4
5
6
7
8
9
10
11
12
&#60;em&#62;Posted in:&#60;/em&#62;
&#60;?php foreach&#40;&#40;get_the_category&#40;&#41;&#41; as $cat&#41;
	&#123;
	$catname =$cat-&#62;category_nicename;
	echo &#34;&#60;a href\&#34;/category/&#34;;
	echo $catname;
	echo &#34;/\&#34;&#62;&#34;;
	echo &#34;&#60;img src\&#34;/wp-content/cat-icons/&#34;;
	echo $catname;
	echo &#34;.png\&#34; alt=\&#34;$catname category image\&#34; border=\&#34;0\&#34; /&#62;&#60;/a&#62;\n&#34;;
	&#125;
?&#62;

代码很简单，就是查询日志所在分类及其缩略名，然后自动在 cat-icons 目录匹配 category_nicename.png，然后输出。当然，你可以更改.png为任何图片格式。
怎么样，很简单吧？！如果你有什么 idea，欢迎来站趣-分享建站的乐趣讨论。
WordPress CMS 技巧：按照分类调用最新文章 (26)站趣新版上线 (16)如何使你的博客支持 iPhone (8)使用 get_avatar 为 WP [...]]]></description>
			<content:encoded><![CDATA[<p>在看了上一篇 <a href="http://eachsite.org/wordpress-cms-tips-latest-posts-by-cat/">WordPress CMS 技巧：按照分类调用最新文章</a>之后，你是不是觉得能为分类添加图片就更完美呢？<br />
我们知道，<a href="http://eachsite.org/tag/wordpress/">WordPress</a> 暂时还不支持为分类添加图片，但这难不倒我们众多的 WordPress 爱好者，我们稍微 DIY  一下 <a href="http://eachsite.org/category/wordpress-themes/themes-intro/">WordPress 主题</a>，一个山寨 WordPress 分类图标就出来了。<br />
思路：我们知道，<strong>get the category</strong> 可以获取 cat_ID（分类 ID）、cat_name（分类名）、category_nicename（分类缩略名）、category_description（分类描述）、category_parent（分类层级） 以及 category_count 等信息，那么，我们就可以用<span id="more-379"></span> category_nicename 命名图片，然后匹配并输出。可能有点拗口，看下代码吧，很简单：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&lt;em&gt;Posted in:&lt;/em&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>get_the_category<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$cat</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$catname</span> <span style="color: #339933;">=</span><span style="color: #000088;">$cat</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">category_nicename</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;a href<span style="color: #000099; font-weight: bold;">\&quot;</span>/category/&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$catname</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;/<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;img src<span style="color: #000099; font-weight: bold;">\&quot;</span>/wp-content/cat-icons/&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$catname</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;.png<span style="color: #000099; font-weight: bold;">\&quot;</span> alt=<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$catname</span> category image<span style="color: #000099; font-weight: bold;">\&quot;</span> border=<span style="color: #000099; font-weight: bold;">\&quot;</span>0<span style="color: #000099; font-weight: bold;">\&quot;</span> /&gt;&lt;/a&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>代码很简单，就是查询日志所在分类及其缩略名，然后自动在 <strong>cat-icons</strong> 目录匹配 category_nicename.png，然后输出。当然，你可以更改.png为任何图片格式。<br />
怎么样，很简单吧？！如果你有什么 idea，欢迎来<a href="http://eachsite.org/">站趣-分享建站的乐趣</a>讨论。</p>
<ul class="related_post"><li><a href="http://eachsite.org/wordpress-cms-tips-latest-posts-by-cat/" title="WordPress CMS 技巧：按照分类调用最新文章">WordPress CMS 技巧：按照分类调用最新文章</a> (26)</li><li><a href="http://eachsite.org/eachsite-new-wp-cms-theme-online/" title="站趣新版上线">站趣新版上线</a> (16)</li><li><a href="http://eachsite.org/how-to-create-an-iphone-version-of-your-blog/" title="如何使你的博客支持 iPhone">如何使你的博客支持 iPhone</a> (8)</li><li><a href="http://eachsite.org/use-getavatar-to-add-gravatar-in-wp/" title="使用 get_avatar 为 WP 自定义 Gravatar 默认头像">使用 get_avatar 为 WP 自定义 Gravatar 默认头像</a> (22)</li><li><a href="http://eachsite.org/add-a-default-gravatar-in-wordpress/" title="如何在 WordPress 中添加 Gravatar 默认头像">如何在 WordPress 中添加 Gravatar 默认头像</a> (11)</li><li><a href="http://eachsite.org/wordpress-cms-theme-comfymagazine/" title="情人节礼物 WordPress CMS 主题 ComFy Magazine">情人节礼物 WordPress CMS 主题 ComFy Magazine</a> (13)</li><li><a href="http://eachsite.org/wordpress-cms-theme-hybrid-news/" title="WordPress CMS 主题 Hybrid News">WordPress CMS 主题 Hybrid News</a> (13)</li><li><a href="http://eachsite.org/wordpress-shortcodes/" title="精通 WordPress 短代码">精通 WordPress 短代码</a> (6)</li><li><a href="http://eachsite.org/free-wordpress-themes-mimbo3/" title="WordPress 免费主题 Mimbo3.0">WordPress 免费主题 Mimbo3.0</a> (6)</li><li><a href="http://eachsite.org/free-wordpress-cms-theme-thestars/" title="Free WordPress CMS 主题 the stars">Free WordPress CMS 主题 the stars</a> (4)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://eachsite.org/show-wordpress-category-images/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>WordPress CMS 技巧：按照分类调用最新文章</title>
		<link>http://eachsite.org/wordpress-cms-tips-latest-posts-by-cat/</link>
		<comments>http://eachsite.org/wordpress-cms-tips-latest-posts-by-cat/#comments</comments>
		<pubDate>Mon, 16 Feb 2009 16:30:59 +0000</pubDate>
		<dc:creator>辐射鱼</dc:creator>
				<category><![CDATA[WordPress 教程]]></category>
		<category><![CDATA[主题开发]]></category>
		<category><![CDATA[进阶使用]]></category>
		<category><![CDATA[cms]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[主题]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://eachsite.org/?p=369</guid>
		<description><![CDATA[
我们知道，许多 WordPress Magazine/News/CMS 主题最吸引人的地方就是在首页按分类摘要显示或列标题显示最新文章。今天，站趣-分享建站的乐趣和大家一起学习这个 WordPress CMS 技巧：按照分类调用最新文章。我们可以将其用在博客首页、404页面、甚至任意存档页面。
下面，我们将以 WordPress 默认主题为例，详细介绍如何按照分类调用最新文章。

首先，建立一个简单的分类存档页面模板
我们希望代码能被多次利用，所以我们选择页面模板这种方式，使其不但可以用在首页，还可以用在404页面或其他任意存档页面。下面，我们就一起来为 WordPress 默认主题添加一个名为 Category Archive 的页面模板。使用你最习惯的文本编辑器，如 Notepad++、UltraEdite 等等，复制以下代码并保存为 category-archive.php 或其他你喜欢的名字：

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
&#60;?php
/*
Template Name: Category Archive
*/
?&#62;
&#160;
&#60;?php get_header&#40;&#41;; ?&#62;
&#160;
&#60;div id=&#34;content&#34; class=&#34;narrowcolumn&#34;&#62;
&#160;
	&#60;?php if &#40;have_posts&#40;&#41;&#41; : while &#40;have_posts&#40;&#41;&#41; : the_post&#40;&#41;; ?&#62;
	&#60;div class=&#34;post&#34; id=&#34;post-&#60;?php the_ID&#40;&#41;; ?&#62;&#34;&#62;
	&#60;h2&#62;&#60;?php the_title&#40;&#41;; ?&#62;&#60;/h2&#62;
		&#60;div class=&#34;entry&#34;&#62;
			&#60;?php the_content&#40;'&#60;p class=&#34;serif&#34;&#62;Read the rest of this page &#38;raquo;&#60;/p&#62;'&#41;; ?&#62;
&#160;
			&#60;?php wp_link_pages&#40;array&#40;'before' =&#62; '&#60;p&#62;&#60;strong&#62;Pages:&#60;/strong&#62; ', 'after' =&#62; [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://eachsite.org/wp-content/eachsite/2009/02/latest-posts-by-category-archive-263x300.gif" alt="WordPress CMS 技巧：按照分类调用最新文章列表" title="WordPress CMS 技巧：按照分类调用最新文章列表" width="263" height="300" class="alignright size-medium wp-image-372" /><br />
我们知道，许多 WordPress Magazine/News/CMS 主题最吸引人的地方就是在首页按分类摘要显示或列标题显示最新文章。今天，<a href="http://eachsite.org">站趣-分享建站的乐趣</a>和大家一起学习这个 WordPress CMS 技巧：<strong>按照分类调用最新文章</strong>。我们可以将其用在博客首页、404页面、甚至任意存档页面。<br />
下面，我们将以 <a href="http://eachsite.org/tag/wordpress/">WordPress</a> 默认主题为例，详细介绍如何按照分类调用最新文章。</p>
<ol>
<li>首先，建立一个简单的分类存档页面模板</li>
<p>我们希望代码能被多次利用，所以我们选择页面模板这种方式，使其不但可以用在首页，还可以用在404页面或其他任意存档页面。下面，我们就一起来为 WordPress 默认主题添加一个名为 <strong>Category Archive</strong> 的页面模板。使用你最习惯的文本编辑器，如 Notepad++、UltraEdite 等等，复制以下代码并保存为 category-archive.php 或其他你喜欢的名字：<span id="more-369"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*
Template Name: Category Archive
*/</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> get_header<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;div id=&quot;content&quot; class=&quot;narrowcolumn&quot;&gt;
&nbsp;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> the_post<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;div class=&quot;post&quot; id=&quot;post-<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_ID<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
	&lt;h2&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/h2&gt;
		&lt;div class=&quot;entry&quot;&gt;
			<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_content<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;p class=&quot;serif&quot;&gt;Read the rest of this page &amp;raquo;&lt;/p&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">&lt;?php</span> wp_link_pages<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'before'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;p&gt;&lt;strong&gt;Pages:&lt;/strong&gt; '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'after'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;/p&gt;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'next_or_number'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'number'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
		&lt;/div&gt;
	&lt;/div&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
	&lt;!-- Category Archive Start --&gt;
	&lt;!-- Category Archive End --&gt;
&nbsp;
&lt;/div&gt;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> get_sidebar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> get_footer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<li>第二步，增加按分类查询代码</li>
<p>复制以下代码到 <!-- Category Archive Start -->、<!-- Category Archive End --> 之间：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">	&lt;ul class=&quot;catArchive&quot;&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #000088;">$catQuery</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_results</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM <span style="color: #006699; font-weight: bold;">$wpdb-&gt;terms</span> AS wterms INNER JOIN <span style="color: #006699; font-weight: bold;">$wpdb-&gt;term_taxonomy</span> AS wtaxonomy ON ( wterms.term_id = wtaxonomy.term_id ) WHERE wtaxonomy.taxonomy = 'category' AND wtaxonomy.parent = 0 AND wtaxonomy.count &gt; 0 AND wterms.term_id NOT IN (2,5,6)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$catCounter</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$catQuery</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$category</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000088;">$catCounter</span><span style="color: #339933;">++;</span>
&nbsp;
		<span style="color: #000088;">$catStyle</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_int</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$catCounter</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$catStyle</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">' class=&quot;catAlt&quot;'</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$catLink</span> <span style="color: #339933;">=</span> get_category_link<span style="color: #009900;">&#40;</span><span style="color: #000088;">$category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">term_id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;li'</span><span style="color: #339933;">.</span><span style="color: #000088;">$catStyle</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&gt;&lt;h3&gt;&lt;a href=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$catLink</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot; title=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/a&gt;&lt;/h3&gt;'</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;ul&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
			query_posts<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cat='</span><span style="color: #339933;">.</span><span style="color: #000088;">$category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">term_id</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;showposts=5'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> the_post<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
				&lt;li&gt;&lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_permalink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; rel=&quot;bookmark&quot; title=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/a&gt;&lt;/li&gt;
			<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
				&lt;li&gt;&lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$catLink</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; title=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;More &lt;strong&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
			&lt;/ul&gt;
		&lt;/li&gt;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #009900;">&#125;</span>	<span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;/ul&gt;</pre></td></tr></table></div>

<p><strong>代码解析：</strong>此段代码会从 WordPress 数据库中查询除分类 id 为2、5、6的所有分类，并列表显示其最新5篇文章。当然，我们稍微改动一下查询语句，使其只查询 id 为2、5、6的分类。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;">$catQuery <span style="color: #66cc66;">=</span> $wpdb<span style="color: #66cc66;">-&gt;</span>get_results<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;SELECT * FROM $wpdb-&gt;terms AS wterms INNER JOIN $wpdb-&gt;term_taxonomy AS wtaxonomy ON ( wterms.term_id = wtaxonomy.term_id ) WHERE wtaxonomy.taxonomy = 'category' AND wtaxonomy.parent = 0 AND wtaxonomy.count &gt; 0 AND wterms.term_id IN (2,5,6)&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>分列显示最新文章</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$catCounter</span><span style="color: #339933;">++;</span>
<span style="color: #000088;">$catStyle</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_int</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$catCounter</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$catStyle</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">' class=&quot;catAlt&quot;'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>查询数据库</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$catQuery</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$category</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">/* Code used to retrieve and display the latest posts */</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>显示分类标题</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;li'</span><span style="color: #339933;">.</span><span style="color: #000088;">$catStyle</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&gt;&lt;h3&gt;&lt;a href=&quot;'</span><span style="color: #339933;">.</span>get_category_link<span style="color: #009900;">&#40;</span><span style="color: #000088;">$category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">term_id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot; title=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/a&gt;&lt;/h3&gt;'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>列表显示最新5篇文章</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">query_posts('cat='.$category-&gt;term_id.'&amp;showposts=5');?&gt;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> the_post<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;li&gt;&lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_permalink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; rel=&quot;bookmark&quot; title=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/a&gt;&lt;/li&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;li&gt;&lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$catLink</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; title=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;More &lt;strong&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;</pre></td></tr></table></div>

<li>CSS 样式</li>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* Latest posts by category styles */</span>
&nbsp;
<span style="color: #6666ff;">.catArchive</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">450px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">list-style-type</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.catArchive</span> h3 <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">font</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">normal</span> <span style="color: #993333;">bold</span> <span style="color: #933;">18px</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">border-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#666</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">3px</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.catArchive</span> li <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">210px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">30px</span> <span style="color: #933;">30px</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.catArchive</span> ul <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.catArchive</span> li li <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">border-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#ddd</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">4px</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.catAlt</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">margin-right</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span> !important<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>请根据自身实际情况修改<strong>宽度、分类标题、文章列表样式</strong>。
</ol>
<p>怎么样，按照本文一番修改，你的 <a href="http://eachsite.org/category/wordpress-themes/themes-intro/">WordPress 主题</a>是不是也像一个 CMS 了呢？！<br />
本文由辐射鱼翻译自：<a href="http://blogsessive.com/blogging-tools/latest-posts-by-category-archive/">Latest Posts by Category Archive with WordPress</a><br />
站趣译文：<a href="http://eachsite.org/wordpress-cms-tips-latest-posts-by-cat">WordPress CMS 技巧：按照分类调用最新文章</a></p>
<ul class="related_post"><li><a href="http://eachsite.org/show-wordpress-category-images/" title="WordPress CMS 主题技巧：显示 WordPress 分类图片">WordPress CMS 主题技巧：显示 WordPress 分类图片</a> (9)</li><li><a href="http://eachsite.org/eachsite-new-wp-cms-theme-online/" title="站趣新版上线">站趣新版上线</a> (16)</li><li><a href="http://eachsite.org/how-to-create-an-iphone-version-of-your-blog/" title="如何使你的博客支持 iPhone">如何使你的博客支持 iPhone</a> (8)</li><li><a href="http://eachsite.org/use-getavatar-to-add-gravatar-in-wp/" title="使用 get_avatar 为 WP 自定义 Gravatar 默认头像">使用 get_avatar 为 WP 自定义 Gravatar 默认头像</a> (22)</li><li><a href="http://eachsite.org/add-a-default-gravatar-in-wordpress/" title="如何在 WordPress 中添加 Gravatar 默认头像">如何在 WordPress 中添加 Gravatar 默认头像</a> (11)</li><li><a href="http://eachsite.org/wordpress-cms-theme-comfymagazine/" title="情人节礼物 WordPress CMS 主题 ComFy Magazine">情人节礼物 WordPress CMS 主题 ComFy Magazine</a> (13)</li><li><a href="http://eachsite.org/wordpress-cms-theme-hybrid-news/" title="WordPress CMS 主题 Hybrid News">WordPress CMS 主题 Hybrid News</a> (13)</li><li><a href="http://eachsite.org/wordpress-shortcodes/" title="精通 WordPress 短代码">精通 WordPress 短代码</a> (6)</li><li><a href="http://eachsite.org/free-wordpress-themes-mimbo3/" title="WordPress 免费主题 Mimbo3.0">WordPress 免费主题 Mimbo3.0</a> (6)</li><li><a href="http://eachsite.org/free-wordpress-cms-theme-thestars/" title="Free WordPress CMS 主题 the stars">Free WordPress CMS 主题 the stars</a> (4)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://eachsite.org/wordpress-cms-tips-latest-posts-by-cat/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>自动获取 WordPress 缩略图增强应用</title>
		<link>http://eachsite.org/wordpress-post-images-tips/</link>
		<comments>http://eachsite.org/wordpress-post-images-tips/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 17:08:24 +0000</pubDate>
		<dc:creator>辐射鱼</dc:creator>
				<category><![CDATA[WordPress 教程]]></category>
		<category><![CDATA[主题开发]]></category>
		<category><![CDATA[进阶使用]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[主题]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://eachsite.org/?p=233</guid>
		<description><![CDATA[刚发布完自动获取 WordPress 缩略图这篇 WordPress 教程后，我发现原作者对其进行了增强，也许很多同学还没有来得及了解其原理，那么，我还是另开一篇博文讲解吧。
新代码可实现的功能

Hyperlinks
图片链接到文章地址
Thickbox
实现 Thickbox 图片缩放效果
图片分类

Code:
Download the post image code here
使用方法：

1
2
3
&#60;?php the_image&#40;size, class , link to post , link using thickbox, echo result&#41;; ?&#62; 
//in the example video the single post page uses:
&#60;?php the_image&#40;'medium','post-image',false,true&#41;; ?&#62;

函数默认值：

Size — medium
Class — blank
link to post — false
link using thickbox — false
echo result — true

作者还制作了一段视频教程，大家可以看看：

如何使你的博客支持 iPhone (8)使用 get_avatar 为 [...]]]></description>
			<content:encoded><![CDATA[<p>刚发布完<a href="http://eachsite.org/get-1st-images-in-wordpress/">自动获取 WordPress 缩略图</a>这篇 <a href="http://eachsite.org/tag/course/">WordPress 教程</a>后，我发现原作者对其进行了增强，也许很多同学还没有来得及了解其原理，那么，我还是另开一篇博文讲解吧。<br />
<strong>新代码可实现的功能</strong></p>
<ol>
<li>Hyperlinks</li>
<p>图片链接到文章地址</p>
<li>Thickbox</li>
<p>实现 Thickbox 图片缩放效果</p>
<li>图片分类</li>
</ol>
<p><strong>Code:</strong><span id="more-233"></span><br />
<a href="http://eachsite.org/wp-content/eachsite/2009/01/post-image.txt">Download the post image code here</a><br />
<strong>使用方法：</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> the_image<span style="color: #009900;">&#40;</span>size<span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #339933;">,</span> <span style="color: #990000;">link</span> to post <span style="color: #339933;">,</span> <span style="color: #990000;">link</span> using thickbox<span style="color: #339933;">,</span> <span style="color: #b1b100;">echo</span> result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
//in the example video the single post page uses:
<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_image<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'medium'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'post-image'</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>函数默认值：</p>
<ul>
<li>Size — medium</li>
<li>Class — blank</li>
<li>link to post — false</li>
<li>link using thickbox — false</li>
<li>echo result — true</li>
</ul>
<p>作者还制作了一段视频教程，大家可以看看：<br />
<object width="600" height="400" data="http://www.viddler.com/simple/1596b962/" type="application/x-shockwave-flash"><param name="src" value="http://www.viddler.com/simple/1596b962/" /></object></p>
<ul class="related_post"><li><a href="http://eachsite.org/how-to-create-an-iphone-version-of-your-blog/" title="如何使你的博客支持 iPhone">如何使你的博客支持 iPhone</a> (8)</li><li><a href="http://eachsite.org/use-getavatar-to-add-gravatar-in-wp/" title="使用 get_avatar 为 WP 自定义 Gravatar 默认头像">使用 get_avatar 为 WP 自定义 Gravatar 默认头像</a> (22)</li><li><a href="http://eachsite.org/add-a-default-gravatar-in-wordpress/" title="如何在 WordPress 中添加 Gravatar 默认头像">如何在 WordPress 中添加 Gravatar 默认头像</a> (11)</li><li><a href="http://eachsite.org/show-wordpress-category-images/" title="WordPress CMS 主题技巧：显示 WordPress 分类图片">WordPress CMS 主题技巧：显示 WordPress 分类图片</a> (9)</li><li><a href="http://eachsite.org/wordpress-cms-tips-latest-posts-by-cat/" title="WordPress CMS 技巧：按照分类调用最新文章">WordPress CMS 技巧：按照分类调用最新文章</a> (26)</li><li><a href="http://eachsite.org/wordpress-shortcodes/" title="精通 WordPress 短代码">精通 WordPress 短代码</a> (6)</li><li><a href="http://eachsite.org/get-1st-images-in-wordpress/" title="自动获取 WordPress 缩略图">自动获取 WordPress 缩略图</a> (12)</li><li><a href="http://eachsite.org/wordpress27-threading-comments/" title="WordPress2.7 嵌套评论">WordPress2.7 嵌套评论</a> (7)</li><li><a href="http://eachsite.org/enhance-theme-suit-wordpress27/" title="如何使你的主题兼容 WordPress2.7">如何使你的主题兼容 WordPress2.7</a> (9)</li><li><a href="http://eachsite.org/safest-way-to-include-plugin-code-in-themes/" title="在 WordPress 主题中安全的调用插件代码">在 WordPress 主题中安全的调用插件代码</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://eachsite.org/wordpress-post-images-tips/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>自动获取 WordPress 缩略图</title>
		<link>http://eachsite.org/get-1st-images-in-wordpress/</link>
		<comments>http://eachsite.org/get-1st-images-in-wordpress/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 13:41:45 +0000</pubDate>
		<dc:creator>辐射鱼</dc:creator>
				<category><![CDATA[WordPress 教程]]></category>
		<category><![CDATA[主题开发]]></category>
		<category><![CDATA[进阶使用]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[主题]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://eachsite.org/?p=228</guid>
		<description><![CDATA[sofish 小盆友在他的博客中提到一直没有找到自动显示 WordPress 缩略图的方法，很多主题也只是通过利用 WordPress 中的自定义字段（custom field）功能来曲线救国。但是，这种方法相当不自动，每次都得复制一遍缩略图地址，不是我们鱼类思维。大家知道，每当我们利用 WordPress 后台上传图片时，WordPress 都会自动为我们创建一个缩略图，那么，我们是否可以自动获取这个缩略图呢？答案是肯定的。下面我们一起来看看这篇 WordPress 教程吧
首先，我们将以下代码复制到你主题的 functions.php 中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function the_image&#40;$size = 'medium' , $class = ''&#41;&#123;
	global $post;
&#160;
	//setup the attachment array
	$att_array = array&#40;
		'post_parent' =&#38;gt; $post-&#38;gt;ID,
		'post_type' =&#38;gt; 'attachment',
		'post_mime_type' =&#38;gt; 'image',
		'order_by' =&#38;gt; 'menu_order'
	&#41;;
&#160;
	//get the post attachments
	$attachments = get_children&#40;$att_array&#41;;
&#160;
	//make sure there are attachments
	if &#40;is_array&#40;$attachments&#41;&#41;&#123;
		//loop through them
		foreach&#40;$attachments as $att&#41;&#123;
			//find the one we want based on [...]]]></description>
			<content:encoded><![CDATA[<p>sofish 小盆友在他的博客中提到一直没有找到自动显示 WordPress 缩略图的方法，很多主题也只是通过利用 WordPress 中的自定义字段（custom field）功能来曲线救国。但是，这种方法相当不自动，每次都得复制一遍缩略图地址，不是我们鱼类思维。大家知道，每当我们利用 WordPress 后台上传图片时，WordPress 都会自动为我们创建一个缩略图，那么，我们是否可以自动获取这个缩略图呢？答案是肯定的。下面我们一起来看看这篇 <a href="http://eachsite.org/tag/course/">WordPress 教程</a>吧<br />
<strong>首先，我们将以下代码复制到你主题的 functions.php 中</strong><span id="more-228"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> the_image<span style="color: #009900;">&#40;</span><span style="color: #000088;">$size</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'medium'</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$post</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//setup the attachment array</span>
	<span style="color: #000088;">$att_array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'post_parent'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000088;">$post</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>ID<span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'post_type'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'attachment'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'post_mime_type'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'image'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'order_by'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'menu_order'</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//get the post attachments</span>
	<span style="color: #000088;">$attachments</span> <span style="color: #339933;">=</span> get_children<span style="color: #009900;">&#40;</span><span style="color: #000088;">$att_array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//make sure there are attachments</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$attachments</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//loop through them</span>
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$attachments</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$att</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//find the one we want based on its characteristics</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$att</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>menu_order <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$image_src_array</span> <span style="color: #339933;">=</span> wp_get_attachment_image_src<span style="color: #009900;">&#40;</span><span style="color: #000088;">$att</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>ID<span style="color: #339933;">,</span> <span style="color: #000088;">$size</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #666666; font-style: italic;">//get url - 1 and 2 are the x and y dimensions</span>
				<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$image_src_array</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$caption</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$att</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>post_excerpt<span style="color: #339933;">;</span>
				<span style="color: #000088;">$image_html</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;img class=&quot;%s&quot; src=&quot;%s&quot; alt=&quot;%s&quot; /&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #666666; font-style: italic;">//combine the data</span>
				<span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image_html</span><span style="color: #339933;">,</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span><span style="color: #000088;">$caption</span><span style="color: #339933;">,</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #666666; font-style: italic;">//echo the result</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$html</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>以上代码会自动列出 WordPress 文章中的所有附件图片，并显示第一张图片的缩略图。<br />
<strong>接下来，我们只需在 index.php 等文件中添加以下代码，获取 WordPress 缩略图</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> the_image<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'thumbnail'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'post-thumb'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>当然，我们也可以显示中等尺寸的缩略图</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> the_image<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'medium'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'post-image'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>当然，以上代码还有一点点效率问题，如果你有更好的 idea 只匹配第一张图片并显示其缩略图，请 share with us！！！<br />
本文译自：<a href="http://www.wp-fun.co.uk/2009/01/08/post-image-the-easy-peasy-way/">Post image the easy peasy way</a><br />
中文翻译：<a href="http://eachsite.org/get-1st-images-in-wordpress/">自动获取 WordPress 缩略图</a></p>
<ul class="related_post"><li><a href="http://eachsite.org/how-to-create-an-iphone-version-of-your-blog/" title="如何使你的博客支持 iPhone">如何使你的博客支持 iPhone</a> (8)</li><li><a href="http://eachsite.org/use-getavatar-to-add-gravatar-in-wp/" title="使用 get_avatar 为 WP 自定义 Gravatar 默认头像">使用 get_avatar 为 WP 自定义 Gravatar 默认头像</a> (22)</li><li><a href="http://eachsite.org/add-a-default-gravatar-in-wordpress/" title="如何在 WordPress 中添加 Gravatar 默认头像">如何在 WordPress 中添加 Gravatar 默认头像</a> (11)</li><li><a href="http://eachsite.org/show-wordpress-category-images/" title="WordPress CMS 主题技巧：显示 WordPress 分类图片">WordPress CMS 主题技巧：显示 WordPress 分类图片</a> (9)</li><li><a href="http://eachsite.org/wordpress-cms-tips-latest-posts-by-cat/" title="WordPress CMS 技巧：按照分类调用最新文章">WordPress CMS 技巧：按照分类调用最新文章</a> (26)</li><li><a href="http://eachsite.org/wordpress-shortcodes/" title="精通 WordPress 短代码">精通 WordPress 短代码</a> (6)</li><li><a href="http://eachsite.org/wordpress-post-images-tips/" title="自动获取 WordPress 缩略图增强应用">自动获取 WordPress 缩略图增强应用</a> (5)</li><li><a href="http://eachsite.org/wordpress27-threading-comments/" title="WordPress2.7 嵌套评论">WordPress2.7 嵌套评论</a> (7)</li><li><a href="http://eachsite.org/enhance-theme-suit-wordpress27/" title="如何使你的主题兼容 WordPress2.7">如何使你的主题兼容 WordPress2.7</a> (9)</li><li><a href="http://eachsite.org/safest-way-to-include-plugin-code-in-themes/" title="在 WordPress 主题中安全的调用插件代码">在 WordPress 主题中安全的调用插件代码</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://eachsite.org/get-1st-images-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>WordPress2.7 嵌套评论</title>
		<link>http://eachsite.org/wordpress27-threading-comments/</link>
		<comments>http://eachsite.org/wordpress27-threading-comments/#comments</comments>
		<pubDate>Mon, 29 Dec 2008 19:03:34 +0000</pubDate>
		<dc:creator>辐射鱼</dc:creator>
				<category><![CDATA[WordPress 主题]]></category>
		<category><![CDATA[主题开发]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[主题]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://eachsite.org/?p=161</guid>
		<description><![CDATA[在如何使你的主题兼容 WordPress2.7 这篇文章中，我们谈到了 WordPress2.7 令人兴奋的嵌套评论功能，如果你的主题还不支持，那么我们通过本篇 WordPress 教程，一起来看看如何实现它吧！！！
WordPress2.7 使用了一个新函数&#8211;wp_list_comments，使用它，我们即可实现嵌套评论（Threading Comments）这个功能。那，我们开始改造 comments.php 文件吧：

首先，添加 comment-reply JavaScript，让嵌套评论 （Threading comments） 能够正常运行
打开 header.php 文件，在 wp_head() 函数之前添加如下函数：

&#60;?php if &#40; is_singular&#40;&#41; &#41; wp_enqueue_script&#40; 'comment-reply' &#41;; ?&#62;

WordPress2.7 全新的 comments loop 介绍
密码保护检查

1
2
3
4
5
6
7
8
9
&#60;?php
  if &#40;!empty&#40;$_SERVER&#91;'SCRIPT_FILENAME'&#93;&#41; &#38;&#38; 'comments.php' == basename&#40;$_SERVER&#91;'SCRIPT_FILENAME'&#93;&#41;&#41;
      die &#40;'Please do not load this page directly. Thanks!'&#41;;
  if [...]]]></description>
			<content:encoded><![CDATA[<p>在<a href="http://eachsite.org/enhance-theme-suit-wordpress27/">如何使你的主题兼容 WordPress2.7</a> 这篇文章中，我们谈到了 WordPress2.7 令人兴奋的嵌套评论功能，如果你的主题还不支持，那么我们通过本篇 <a href="http://eachsite.org/category/wordpress-course/">WordPress 教程</a>，一起来看看如何实现它吧！！！<br />
WordPress2.7 使用了一个新函数&#8211;wp_list_comments，使用它，我们即可实现嵌套评论（Threading Comments）这个功能。那，我们开始改造 comments.php 文件吧：</p>
<ol>
<li>首先，添加 comment-reply JavaScript，让嵌套评论 （Threading comments） 能够正常运行</li>
<p>打开 header.php 文件，在 wp_head() 函数之前添加如下函数：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> is_singular<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> wp_enqueue_script<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'comment-reply'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<li>WordPress2.7 全新的 comments loop 介绍</li>
<p>密码保护检查<span id="more-161"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'SCRIPT_FILENAME'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #0000ff;">'comments.php'</span> <span style="color: #339933;">==</span> <span style="color: #990000;">basename</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'SCRIPT_FILENAME'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #990000;">die</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Please do not load this page directly. Thanks!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> post_password_required<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
      &lt;p class=&quot;nocomments&quot;&gt;This post is password protected. Enter the password to view comments.&lt;/p&gt;
  <span style="color: #000000; font-weight: bold;">&lt;?php</span>
      <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>WordPress 2.7 的评论 Loop</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> have_comments<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;h3 id=&quot;comments&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> comments_number<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No Responses'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'One Response'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'% Responses'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span> to &amp;#8220;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&amp;#8221;&lt;/h3&gt;
&nbsp;
	&lt;ol class=&quot;commentlist&quot;&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> wp_list_comments<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;/ol&gt;
	&lt;div class=&quot;navigation&quot;&gt;
		&lt;div class=&quot;alignleft&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> previous_comments_link<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/div&gt;
		&lt;div class=&quot;alignright&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> next_comments_link<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/div&gt;
	&lt;/div&gt;
 <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">else</span> <span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">// this is displayed if there are no comments so far ?&gt;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'open'</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comment_status</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
		&lt;!-- If comments are open, but there are no comments. --&gt;
&nbsp;
	 <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">else</span> <span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">// comments are closed ?&gt;</span>
		&lt;!-- If comments are closed. --&gt;
		&lt;p class=&quot;nocomments&quot;&gt;Comments are closed.&lt;/p&gt;
&nbsp;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<li>CSS 样式</li>
<p>至于 CSS 样式，各有各的喜好，大家可以借鉴 <a href="http://eachsite.org/eachblue-v1/">eachBlue</a> 和 <a href="http://www.thirdinfo.com/blueweed-for-wordpress27.htm">Blueweed for WordPress2.7</a> 这两款主题自行定义 children、depth、author。</p>
<li>提交嵌套评论（Threading Comments）</li>
<p>首先需要要把评论框 (Comment Form) 放入一个 ID 为 respond 的 DIV 中，然后找到类似以下代码：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>input name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> tabindex<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;5&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Submit Comment&quot;</span> <span style="color: #339933;">/&gt;</span></pre></div></div>

<p>在其后面一行加入：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> comment_id_fields<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>当然，假若评论者不想回复该评论了，可以加入以下代码，可以取消评论：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&lt;div class=&quot;cancel-comment-reply&quot;&gt;
	&lt;small&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> cancel_comment_reply_link<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/small&gt;
&lt;/div&gt;</pre></td></tr></table></div>

</ol>
<p>经过以上改动，我们的主题就支持 WordPress2.7 的嵌套评论（Threading Comments）功能了，使用者只需在 <a href="http://eachsite.org/tag/wordpress/">WordPress</a> 管理后台激活嵌套评论即可。下一篇文章我们将一起讨论 WordPress2.7 评论分页，敬请期待。<br />
如果你还有什么疑问，请留言或加入<a href="http://eachsite.org/">站趣-分享建站的乐趣</a> <strong>QQ 群：8329980</strong> 交流</p>
<ul class="related_post"><li><a href="http://eachsite.org/how-to-create-an-iphone-version-of-your-blog/" title="如何使你的博客支持 iPhone">如何使你的博客支持 iPhone</a> (8)</li><li><a href="http://eachsite.org/use-getavatar-to-add-gravatar-in-wp/" title="使用 get_avatar 为 WP 自定义 Gravatar 默认头像">使用 get_avatar 为 WP 自定义 Gravatar 默认头像</a> (22)</li><li><a href="http://eachsite.org/add-a-default-gravatar-in-wordpress/" title="如何在 WordPress 中添加 Gravatar 默认头像">如何在 WordPress 中添加 Gravatar 默认头像</a> (11)</li><li><a href="http://eachsite.org/show-wordpress-category-images/" title="WordPress CMS 主题技巧：显示 WordPress 分类图片">WordPress CMS 主题技巧：显示 WordPress 分类图片</a> (9)</li><li><a href="http://eachsite.org/wordpress-cms-tips-latest-posts-by-cat/" title="WordPress CMS 技巧：按照分类调用最新文章">WordPress CMS 技巧：按照分类调用最新文章</a> (26)</li><li><a href="http://eachsite.org/wordpress-shortcodes/" title="精通 WordPress 短代码">精通 WordPress 短代码</a> (6)</li><li><a href="http://eachsite.org/wordpress-post-images-tips/" title="自动获取 WordPress 缩略图增强应用">自动获取 WordPress 缩略图增强应用</a> (5)</li><li><a href="http://eachsite.org/get-1st-images-in-wordpress/" title="自动获取 WordPress 缩略图">自动获取 WordPress 缩略图</a> (12)</li><li><a href="http://eachsite.org/enhance-theme-suit-wordpress27/" title="如何使你的主题兼容 WordPress2.7">如何使你的主题兼容 WordPress2.7</a> (9)</li><li><a href="http://eachsite.org/safest-way-to-include-plugin-code-in-themes/" title="在 WordPress 主题中安全的调用插件代码">在 WordPress 主题中安全的调用插件代码</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://eachsite.org/wordpress27-threading-comments/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>如何使你的主题兼容 WordPress2.7</title>
		<link>http://eachsite.org/enhance-theme-suit-wordpress27/</link>
		<comments>http://eachsite.org/enhance-theme-suit-wordpress27/#comments</comments>
		<pubDate>Mon, 29 Dec 2008 09:52:20 +0000</pubDate>
		<dc:creator>辐射鱼</dc:creator>
				<category><![CDATA[WordPress 主题]]></category>
		<category><![CDATA[主题开发]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[主题]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://eachsite.org/?p=159</guid>
		<description><![CDATA[WordPress2.7 正式版已经发布两周多了，可许多主题作者仍然没有升级主题，使其完全支持 WordPress2.7。你是否只能眼睁睁看着 WordPress2.7 的嵌套评论（Threading Comments）、评论分页（Paging Comments）、日志置顶（post sticky）等诱人的功能而望洋兴叹呢？
不要垂头丧气，站趣-分享建站的乐趣就来介绍几个 WordPress 教程，用于改进你心爱的主题，使其支持 WordPress2.7。

WordPress2.7 嵌套评论（Threading Comments）
WordPress2.7 最令人欣喜的功能莫过于嵌套评论（Threading Comments）这个功能了，站趣-分享建站的乐趣制作的 eachBlue 和 Blueweed for WprdPress2.7 这两个主题业已支持该功能，如果你的主题还不支持，强烈建议你下载试用，当然，你也可以和我一起，将主题彻底改造一下。效果图如下：

WordPress2.7 评论分页(Paging Comments)
WordPress 长长的评论页会将博客拖慢不少，WordPress2.7 之前，我们只有借助于插件才可以将评论分页，现在，我们可以使用 WordPress2.7 原生的 评论分页(Paging Comments)了。
Post Classes
WordPress2.7 推出之前，我们通常使用以下代码定义 Post Classes：

&#60;div id=&#34;post-&#60;?php the_ID&#40;&#41;; ?&#62;&#34; class=&#34;post&#34;&#62;

但在 WordPress2.7 中，WordPress 开发团队增强了 Post Classes 的功能，主题作者可以通过 Post Classes 实现文章置顶等功能。 使用方法：

&#60;div id=&#34;post-&#60;?php the_ID&#40;&#41;; ?&#62;&#34; &#60;?php post_class&#40;&#41;; ?&#62;&#62;

我们甚至可以自定义 class 名称：

&#60;div [...]]]></description>
			<content:encoded><![CDATA[<p>WordPress2.7 正式版已经发布两周多了，可许多主题作者仍然没有升级主题，使其完全支持 WordPress2.7。你是否只能眼睁睁看着 WordPress2.7 的嵌套评论（Threading Comments）、评论分页（Paging Comments）、日志置顶（post sticky）等诱人的功能而望洋兴叹呢？<br />
不要垂头丧气，<a href="http://eachsite.org/">站趣-分享建站的乐趣</a>就来介绍几个 <a href="http://eachsite.org/category/wordpress-course/">WordPress 教程</a>，用于改进你心爱的主题，使其支持 WordPress2.7。</p>
<ol>
<li><a href="http://eachsite.org/wordpress27-threading-comments/">WordPress2.7 嵌套评论（Threading Comments）</a></li>
<p>WordPress2.7 最令人欣喜的功能莫过于<a href="http://eachsite.org/wordpress27-threading-comments/">嵌套评论（Threading Comments）</a>这个功能了，<a href="http://eachsite.org/">站趣-分享建站的乐趣</a>制作的 <a href="http://eachsite.org/eachblue-v1/">eachBlue</a> 和 <a href="http://www.thirdinfo.com/blueweed-for-wordpress27.htm">Blueweed for WprdPress2.7</a> 这两个主题业已支持该功能，如果你的主题还不支持，强烈建议你下载试用，当然，你也可以和我一起，将主题彻底改造一下。效果图如下：<span id="more-159"></span><br />
<img src="http://eachsite.org/wp-content/eachsite/2008/12/eachblue-comments-300x244.png" alt="eachBlue threading comments and paging comments" /></p>
<li><a href="http://eachsite.org/how-to-page-wp27-comments/">WordPress2.7 评论分页(Paging Comments)</a></li>
<p>WordPress 长长的评论页会将博客拖慢不少，WordPress2.7 之前，我们只有借助于插件才可以将评论分页，现在，我们可以使用 WordPress2.7 原生的 评论分页(Paging Comments)了。</p>
<li>Post Classes</li>
<p>WordPress2.7 推出之前，我们通常使用以下代码定义 Post Classes：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;div id=&quot;post-<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_ID<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; class=&quot;post&quot;&gt;</pre></div></div>

<p>但在 WordPress2.7 中，WordPress 开发团队增强了 Post Classes 的功能，主题作者可以通过 Post Classes 实现文章置顶等功能。 使用方法：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;div id=&quot;post-<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_ID<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; <span style="color: #000000; font-weight: bold;">&lt;?php</span> post_class<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&gt;</pre></div></div>

<p>我们甚至可以自定义 class 名称：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;div id=&quot;post-<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_ID<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; <span style="color: #000000; font-weight: bold;">&lt;?php</span> post_class<span style="color: #009900;">&#40;</span>style<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&gt;</pre></div></div>

<p>style 可以是 post、category-slug、tag-slug、sticky(置顶)等值。</p>
<li>Logout Link</li>
<p>WordPress2.7 新增了 wp_logout_url 这个函数，我们可以在主题中使用它来注销登陆。使用方法：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> wp_logout_url<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;Logout&lt;/a&gt;</pre></div></div>

<li>Search form</li>
<p>WordPress2.7 对调用搜索函数也进行了改动，之前，我们都是使用以下代码调用搜索框：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">include</span> <span style="color: #009900;">&#40;</span>TEMPLATEPATH<span style="color: #339933;">.</span><span style="color: #0000ff;">'/searchform.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>现在，我们可以使用更简洁的 get_search_form 函数调用 searchform.php 文件了，使用方法：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> get_search_form<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

</ol>
<p>文章很长，但是，为了体验 WordPress2.7 的新功能，折腾一下是值得的，你说呢？如果你还有什么疑问，请留言。</p>
<ul class="related_post"><li><a href="http://eachsite.org/how-to-create-an-iphone-version-of-your-blog/" title="如何使你的博客支持 iPhone">如何使你的博客支持 iPhone</a> (8)</li><li><a href="http://eachsite.org/use-getavatar-to-add-gravatar-in-wp/" title="使用 get_avatar 为 WP 自定义 Gravatar 默认头像">使用 get_avatar 为 WP 自定义 Gravatar 默认头像</a> (22)</li><li><a href="http://eachsite.org/add-a-default-gravatar-in-wordpress/" title="如何在 WordPress 中添加 Gravatar 默认头像">如何在 WordPress 中添加 Gravatar 默认头像</a> (11)</li><li><a href="http://eachsite.org/show-wordpress-category-images/" title="WordPress CMS 主题技巧：显示 WordPress 分类图片">WordPress CMS 主题技巧：显示 WordPress 分类图片</a> (9)</li><li><a href="http://eachsite.org/wordpress-cms-tips-latest-posts-by-cat/" title="WordPress CMS 技巧：按照分类调用最新文章">WordPress CMS 技巧：按照分类调用最新文章</a> (26)</li><li><a href="http://eachsite.org/wordpress-shortcodes/" title="精通 WordPress 短代码">精通 WordPress 短代码</a> (6)</li><li><a href="http://eachsite.org/wordpress-post-images-tips/" title="自动获取 WordPress 缩略图增强应用">自动获取 WordPress 缩略图增强应用</a> (5)</li><li><a href="http://eachsite.org/get-1st-images-in-wordpress/" title="自动获取 WordPress 缩略图">自动获取 WordPress 缩略图</a> (12)</li><li><a href="http://eachsite.org/wordpress27-threading-comments/" title="WordPress2.7 嵌套评论">WordPress2.7 嵌套评论</a> (7)</li><li><a href="http://eachsite.org/safest-way-to-include-plugin-code-in-themes/" title="在 WordPress 主题中安全的调用插件代码">在 WordPress 主题中安全的调用插件代码</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://eachsite.org/enhance-theme-suit-wordpress27/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>在 WordPress 主题中安全的调用插件代码</title>
		<link>http://eachsite.org/safest-way-to-include-plugin-code-in-themes/</link>
		<comments>http://eachsite.org/safest-way-to-include-plugin-code-in-themes/#comments</comments>
		<pubDate>Mon, 29 Dec 2008 04:58:54 +0000</pubDate>
		<dc:creator>辐射鱼</dc:creator>
				<category><![CDATA[WordPress 主题]]></category>
		<category><![CDATA[主题开发]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[主题]]></category>
		<category><![CDATA[插件]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://eachsite.org/?p=150</guid>
		<description><![CDATA[WordPress 的迷人之处莫不在于其强大、方便的插件功能，很多 WordPress 主题作者，为了某些特殊功能，也会或多或少在主题中使用插件。也许是为了减少代码，很多主题制作者在调用 WordPress 插件代码的时候，使用了类似下面的代码：

1
2
&#60;h2&#62;Plugin Title&#60;/h2&#62;
&#60;?php plugin_template_tag_function&#40;&#41;; ?&#62;

如果主题使用这恰好启用了该插件，那么上面这段代码不会有任何问题，但是，如果主题使用没有启用该插件，或者压根儿就没有安装这个插件，那么，它将引起一些致命的 php 错误。
那么，我们怎样才能更安全的调用插件代码呢？请看以下代码：

1
2
3
4
&#60;?php if &#40; function_exists&#40;’plugin_template_tag_function’&#41; &#41; : ?&#62;
&#60;h2&#62;Plugin Title&#60;/h2&#62;
&#60;?php plugin_template_tag_function&#40;&#41;; ?&#62;
&#60;?php endif; ?&#62;

这段代码在工作之前，会先检查是否存在该插件，如果存在，则执行代码；如果不存在，则自动终止。这样，即使对方没有启用相应的插件，也不会引起 PHP 错误了。
如果您还在使用类似于第一段的代码调用 WordPress 插件，那赶紧更正吧！
如何使你的博客支持 iPhone (8)使用 get_avatar 为 WP 自定义 Gravatar 默认头像 (22)如何在 WordPress 中添加 Gravatar 默认头像 (11)WordPress CMS 主题技巧：显示 WordPress 分类图片 (9)WordPress CMS 技巧：按照分类调用最新文章 (26)精通 WordPress 短代码 (6)自动获取 WordPress 缩略图增强应用 (5)自动获取 WordPress [...]]]></description>
			<content:encoded><![CDATA[<p>WordPress 的迷人之处莫不在于其强大、方便的插件功能，很多 <a href="http://eachsite.org/category/wordpress-themes/">WordPress 主题</a>作者，为了某些特殊功能，也会或多或少在主题中使用插件。也许是为了减少代码，很多主题制作者在调用 <a href="http://eachsite.org/category/wordpress-plugins/">WordPress 插件</a>代码的时候，使用了类似下面的代码：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&lt;h2&gt;Plugin Title&lt;/h2&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> plugin_template_tag_function<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>如果主题使用这恰好启用了该插件，那么上面这段代码不会有任何问题，但是，如果主题使用没有启用该插件，或者压根儿就没有安装这个插件，那么，它将引起一些致命的 php 错误。<br />
那么，我们怎样才能更安全的调用插件代码呢？请看以下代码：<span id="more-150"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span>’plugin_template_tag_function’<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;h2&gt;Plugin Title&lt;/h2&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> plugin_template_tag_function<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>这段代码在工作之前，会先检查是否存在该插件，如果存在，则执行代码；如果不存在，则自动终止。这样，即使对方没有启用相应的插件，也不会引起 PHP 错误了。<br />
如果您还在使用类似于第一段的代码调用 <a href="http://eachsite.org/category/wordpress-plugins/">WordPress 插件</a>，那赶紧更正吧！</p>
<ul class="related_post"><li><a href="http://eachsite.org/how-to-create-an-iphone-version-of-your-blog/" title="如何使你的博客支持 iPhone">如何使你的博客支持 iPhone</a> (8)</li><li><a href="http://eachsite.org/use-getavatar-to-add-gravatar-in-wp/" title="使用 get_avatar 为 WP 自定义 Gravatar 默认头像">使用 get_avatar 为 WP 自定义 Gravatar 默认头像</a> (22)</li><li><a href="http://eachsite.org/add-a-default-gravatar-in-wordpress/" title="如何在 WordPress 中添加 Gravatar 默认头像">如何在 WordPress 中添加 Gravatar 默认头像</a> (11)</li><li><a href="http://eachsite.org/show-wordpress-category-images/" title="WordPress CMS 主题技巧：显示 WordPress 分类图片">WordPress CMS 主题技巧：显示 WordPress 分类图片</a> (9)</li><li><a href="http://eachsite.org/wordpress-cms-tips-latest-posts-by-cat/" title="WordPress CMS 技巧：按照分类调用最新文章">WordPress CMS 技巧：按照分类调用最新文章</a> (26)</li><li><a href="http://eachsite.org/wordpress-shortcodes/" title="精通 WordPress 短代码">精通 WordPress 短代码</a> (6)</li><li><a href="http://eachsite.org/wordpress-post-images-tips/" title="自动获取 WordPress 缩略图增强应用">自动获取 WordPress 缩略图增强应用</a> (5)</li><li><a href="http://eachsite.org/get-1st-images-in-wordpress/" title="自动获取 WordPress 缩略图">自动获取 WordPress 缩略图</a> (12)</li><li><a href="http://eachsite.org/wordpress27-threading-comments/" title="WordPress2.7 嵌套评论">WordPress2.7 嵌套评论</a> (7)</li><li><a href="http://eachsite.org/enhance-theme-suit-wordpress27/" title="如何使你的主题兼容 WordPress2.7">如何使你的主题兼容 WordPress2.7</a> (9)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://eachsite.org/safest-way-to-include-plugin-code-in-themes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

