<?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>Domety &#187; The Loop</title>
	<atom:link href="http://domety.com/archives/tag/the-loop/feed/" rel="self" type="application/rss+xml" />
	<link>http://domety.com</link>
	<description>分享软件、互联网应用技巧以及开发技能</description>
	<lastBuildDate>Fri, 03 Feb 2012 14:02:27 +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>the_content是如何输出摘要和全文的</title>
		<link>http://domety.com/archives/224/</link>
		<comments>http://domety.com/archives/224/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 14:27:46 +0000</pubDate>
		<dc:creator>DDBug</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[The Loop]]></category>
		<category><![CDATA[摘要]]></category>

		<guid isPermaLink="false">http://domety.com/?p=224</guid>
		<description><![CDATA[现在每次写文章的时候，已经习惯了在文章和第一段或前两段之后加一个more标签，这样首页的摘要就会显示more标签之前的内容。但是一直都有一个疑问，首页index.php和日志页面single.php都是调用the_content函数，为什么一个显示摘要，一个显示全文呢？
通过阅读wordpress的源代码，发现了这样一段代码
 if ( is_single() &#124;&#124; is_page() &#124;&#124; is_feed() )
  $more = 1;
难道是通过$more变量决定的？再去了解一下the_content的源代码，果然，就是通过$more变量来决定是输出摘要还是输出全文的。为了证实这个结论，我做了一个实验:
 修改index.php文件，在the_content之前强行把$more的值修改为1
$more = 1;
the_content();
结果之前一直显示摘要的首页，现在显示的是全文内容。同样，再去修改singel.php文件，在the_content之前强行把$more的值修改为0
$more = 0;
the_content();
结果日志页面显示的是摘要，而不是全文。
通过以上实验可以看出，the_content函数是输出全文还是输出摘要，是这个全局变量$more决定的。理解了这一点，我们就可以更灵活的运用the_content函数。下面通过一个实例来结束本文。
首页的第一篇文章显示全文，其它文章显示摘要：
&#60;?php if(have_posts() ){
                   $more = -1;
                   while(have_posts()){
                           the_post(); 
                           // ......
                          if($more == -1){
                                $more = 1;
                                the_content();
                                $more = 0;
                          } else {
                                the_content('继续阅读...');
                           }
                          // ......
                   }//end while(have_posts)
             }//end if(have_posts())
   else{
          echo '没有找到相关文章';
   }//end else
  ?&#62;
]]></description>
			<content:encoded><![CDATA[<p>现在每次写文章的时候，已经习惯了在文章和第一段或前两段之后加一个more标签，这样首页的摘要就会显示more标签之前的内容。但是一直都有一个疑问，首页index.php和日志页面single.php都是调用the_content函数，为什么一个显示摘要，一个显示全文呢？<span id="more-224"></span></p>
<p>通过阅读wordpress的源代码，发现了这样一段代码</p>
<pre name="code" class="php"> if ( is_single() || is_page() || is_feed() )
  $more = 1;</pre>
<p>难道是通过$more变量决定的？再去了解一下the_content的源代码，果然，就是通过$more变量来决定是输出摘要还是输出全文的。为了证实这个结论，我做了一个实验:</p>
<p> 修改index.php文件，在the_content之前强行把$more的值修改为1</p>
<pre name="code" class="php">$more = 1;
the_content();</pre>
<p>结果之前一直显示摘要的首页，现在显示的是全文内容。同样，再去修改singel.php文件，在the_content之前强行把$more的值修改为0</p>
<pre name="code" class="php">$more = 0;
the_content();</pre>
<p>结果日志页面显示的是摘要，而不是全文。</p>
<p>通过以上实验可以看出，the_content函数是输出全文还是输出摘要，是这个全局变量$more决定的。理解了这一点，我们就可以更灵活的运用the_content函数。下面通过一个实例来结束本文。</p>
<p>首页的第一篇文章显示全文，其它文章显示摘要：</p>
<pre name="code" class="php">&lt;?php if(have_posts() ){
                   $more = -1;
                   while(have_posts()){
                           the_post(); 
                           // ......
                          if($more == -1){
                                $more = 1;
                                the_content();
                                $more = 0;
                          } else {
                                the_content('继续阅读...');
                           }
                          // ......
                   }//end while(have_posts)
             }//end if(have_posts())
   else{
          echo '没有找到相关文章';
   }//end else
  ?&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://domety.com/archives/224/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>添加keywords和description</title>
		<link>http://domety.com/archives/206/</link>
		<comments>http://domety.com/archives/206/#comments</comments>
		<pubDate>Fri, 01 Jan 2010 15:53:30 +0000</pubDate>
		<dc:creator>DDBug</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[description]]></category>
		<category><![CDATA[keywords]]></category>
		<category><![CDATA[The Loop]]></category>

		<guid isPermaLink="false">http://domety.com/?p=206</guid>
		<description><![CDATA[看到很多人的网站head部分都有keywords和description，据说是seo优化的方法之一。我对seo一窍不通，所以一直也没有在head部分添加keywords和description。还有另外一个原因就是听说现在搜索引擎已经不会考虑keywords了，不知是否属实，还请有seo经验的朋友们指点一二。
想想本站虽然被百度收录，但一直都没有从百度来的流量，不知道会不会是没有keywords和description的原因呢？不管怎么样，还是决定了把这两样都添加到head中，顺便也作为wordpress循环的实例篇之一吧，加强对wordpress循环的理解。
使用文章标签作为keywords
由于上面提到原因，所以我对keywords也没太上心的去做，除了单篇日志使用日志的标签来作为keywords之外，其它页面一律使用固定的keywords。下面是单篇日志页面使用标签作为keywords的方法
&#60;meta name="keywords" content="&#60;?php echo strip_tags(get_the_tag_list('' , ',' )); ?&#62;" /&#62;
稍微对代码做一下解释：get_the_tag_list函数获取日志的标签列表(形如 &#60;a href=&#8221;标签链接&#8221;&#62;标签名&#60;/a&#62; 这样的标签组合)，然后使用php的strip_tags方法过滤掉html标签（比如&#60;a href=&#8221;"&#62;&#60;/a&#62;这些），从而得到的是只包含标签名的字符串。
description的内容
可能每个人对description的要求都有所不同，以下只是我个人的想法。
一、对于首页，使用固定的description。
&#60;?php $des = "分享软件、互联网应用技巧以及开发技能"; ?&#62;
二、对于单篇的日志页面，我使用日志的摘要作为description,但考虑到摘要的内容可能会太长的原因，所以决定使用字符串截取的方式，以日志的前100个字作为description
&#60;?php $des = mb_strimwidth(strip_tags($post-&#62;post_content),0,100); ?&#62;
三、对于其它页面，比如分类、标签、搜索等，使用页面内所有文章的标题作为description
  if(have_posts()){
   $titles = array();
   while(have_posts()) {
    the_post();
    $titles[] = get_the_title();
   }
   $des = implode(',',$titles);
  }
这里我使用了一个循环，有趣的是这个循环并不会影响页面内的主循环，这是为什么呢？自己思考一下吧，我在前两篇文章中有做过解释。
附完整代码
&#60;?php
 $keywords = "wordpress,html,css,php,windows,程序设计,软件应用,网络应用";
 $des = "分享软件、互联网应用技巧以及开发技能";
 if(is_single() &#124;&#124; is_page()){
  $keywords = strip_tags(get_the_tag_list('',','));
  $des = mb_strimwidth(strip_tags($post-&#62;post_content),0,100);
 }
 elseif(!is_home()){
  if(have_posts()){
   $titles = array();
   while(have_posts()) {
    the_post();
    $titles[] = get_the_title();
   }
   $des = implode(',',$titles);
  }
 }
 ?&#62;
 &#60;meta name="keywords" content="&#60;?php echo $keywords; ?&#62;" /&#62;
 &#60;meta name="decription" content="&#60;?php echo [...]]]></description>
			<content:encoded><![CDATA[<p>看到很多人的网站head部分都有keywords和description，据说是seo优化的方法之一。我对seo一窍不通，所以一直也没有在head部分添加keywords和description。还有另外一个原因就是听说现在搜索引擎已经不会考虑keywords了，不知是否属实，还请有seo经验的朋友们指点一二。</p>
<p>想想本站虽然被百度收录，但一直都没有从百度来的流量，不知道会不会是没有keywords和description的原因呢？不管怎么样，还是决定了把这两样都添加到head中，顺便也作为wordpress循环的实例篇之一吧，加强对wordpress循环的理解。<span id="more-206"></span></p>
<h3>使用文章标签作为keywords</h3>
<p>由于上面提到原因，所以我对keywords也没太上心的去做，除了单篇日志使用日志的标签来作为keywords之外，其它页面一律使用固定的keywords。下面是单篇日志页面使用标签作为keywords的方法</p>
<pre>&lt;meta name="keywords" content="&lt;?php echo strip_tags(get_the_tag_list('' , ',' )); ?&gt;" /&gt;</pre>
<p>稍微对代码做一下解释：get_the_tag_list函数获取日志的标签列表(形如 &lt;a href=&#8221;标签链接&#8221;&gt;标签名&lt;/a&gt; 这样的标签组合)，然后使用php的strip_tags方法过滤掉html标签（比如&lt;a href=&#8221;"&gt;&lt;/a&gt;这些），从而得到的是只包含标签名的字符串。</p>
<h3>description的内容</h3>
<p>可能每个人对description的要求都有所不同，以下只是我个人的想法。</p>
<p>一、对于首页，使用固定的description。</p>
<pre>&lt;?php $des = "分享软件、互联网应用技巧以及开发技能"; ?&gt;</pre>
<p>二、对于单篇的日志页面，我使用日志的摘要作为description,但考虑到摘要的内容可能会太长的原因，所以决定使用字符串截取的方式，以日志的前100个字作为description</p>
<pre>&lt;?php $des = mb_strimwidth(strip_tags($post-&gt;post_content),0,100); ?&gt;</pre>
<p>三、对于其它页面，比如分类、标签、搜索等，使用页面内所有文章的标题作为description</p>
<pre>  if(have_posts()){
   $titles = array();
   while(have_posts()) {
    the_post();
    $titles[] = get_the_title();
   }
   $des = implode(',',$titles);
  }</pre>
<p>这里我使用了一个循环，有趣的是这个循环并不会影响页面内的主循环，这是为什么呢？自己思考一下吧，我在前两篇文章中有做过解释。</p>
<h3>附完整代码</h3>
<pre>&lt;?php
 $keywords = "wordpress,html,css,php,windows,程序设计,软件应用,网络应用";
 $des = "分享软件、互联网应用技巧以及开发技能";
 if(is_single() || is_page()){
  $keywords = strip_tags(get_the_tag_list('',','));
  $des = mb_strimwidth(strip_tags($post-&gt;post_content),0,100);
 }
 elseif(!is_home()){
  if(have_posts()){
   $titles = array();
   while(have_posts()) {
    the_post();
    $titles[] = get_the_title();
   }
   $des = implode(',',$titles);
  }
 }
 ?&gt;
 &lt;meta name="keywords" content="&lt;?php echo $keywords; ?&gt;" /&gt;
 &lt;meta name="decription" content="&lt;?php echo $des; ?&gt;" /&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://domety.com/archives/206/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>wordpress循环之查询篇</title>
		<link>http://domety.com/archives/204/</link>
		<comments>http://domety.com/archives/204/#comments</comments>
		<pubDate>Thu, 31 Dec 2009 16:26:32 +0000</pubDate>
		<dc:creator>DDBug</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[The Loop]]></category>
		<category><![CDATA[查询]]></category>

		<guid isPermaLink="false">http://domety.com/?p=204</guid>
		<description><![CDATA[如果说循环是wordpress模板系统的核心，那么查询就是循环的基础。没有查询得到的结果，循环就毫无意义，应该说循环根本就不会开始。但是查询出的结果又需要通过循环来把结果按照我们设计的格式显示出来。这就是两者之间的关系，今天就和大家介绍几种查询的方法。
默认查询
这种查询是自动发生的，几乎发生在我们的每次请求当中：当我们点击网站主页的时候、当我们点击某个分类的时候、当我们点击某个标签的时候&#8230;&#8230;默认的查询就已经开始执行，我们只需要实现循环，就可以把结果显示出来。
这个默认的查询保存在一个全局变量$wp_query之中，查询的文章结果保存在全局变量$posts之中（也可以说保存在$wp_query-&#62;posts中）。当我们执行循环的时候，当前文章保存在全局变量$post当中。你可以想一想，像the_title、the_content这样的函数，并没有传递参数，为什么还可以显示不同的文章？其实它们显示的就是$post中保存各个值，如果我们改变了$post，the_content显示的内容也会改变。所以，除非你很明确你要做的事情，不然不要随便改变$posts、$post。
query_posts查询
当我们不满意默认查询的结果，或者说想更改默认查询的时候，query_posts函数就派上了用场。query_posts的作用就是按照我特有的条件，更改默认的查询以返回我们想要的结果。下面举个简单的例子。
经常会碰到这样一种情况，我们不想在首页显示某个分类（或某几个分类）下的文章。可能我们首先想到的方法就是在循环中加上一个判断，如果判断是该分类下的文章就不显示。不错，这个方法是可行的，但是会有一个问题：假设你在后台设置了每页显示10篇文章，首页上有两篇你想过滤掉的分类文章。如果在循环中过滤的话，最终首页只会显示8篇文章，而不是10篇。
而用query_posts可以完美的解决这种问题，假设某个分类的ID为22，下面我们来看一下query_posts是如何从首页中排除该分类下的文章的。
在开始循环之前，加入以下代码
$r = array('category__not_in'=&#62;array(22));
$r = wp_parse_args( $query_string, $r ); //$query_string是默认的查询字符串，如果没有这一句，分页将会失效
  query_posts($r);
这样查询结果中就不包含分类ID为22的文章，如果还有其它分类不想显示（比如分类ID为23），只需要加入到category__not_in数组中即可，分类ID之间用逗号分开。以下给出完整的代码
&#60;?php
$r = array('category__not_in'=&#62;array(22,23));
$r = wp_parse_args( $query_string, $r );
query_posts($r);
if(have_posts()) :
     while(have_posts()) : the_post();
?&#62;
      &#60;p&#62;在这里加入你的处理代码，比如显示文章标题&#60;/p&#62;
      &#60;p&#62;&#60;?php the_title(); ?&#62;&#60;/p&#62;
     &#60;?php endwhile; ?&#62;
&#60;?php endif; ?&#62;
从代码中我们也可以看到，这种查询支持have_posts和the_posts函数，因为query_posts查询改变了默认的查询$wp_query。那么，如果我们再想利用默认查询怎么办呢？很简单，在上面的循环结束之后，调用wp_reset_query即可
&#60;?php wp_reset_query(); ?&#62;
get_posts查询
get_posts只返回查询结果中的posts，不会改变默认的查询，即不会修改$wp_query。也正是因为没有修改$wp_query,因此循环中不能使用hvae_posts和the_post函数。
使用get_posts查询，一般都是使用foreach循环。我们看一下上面的例子使用get_posts查询的实现方法
&#60;?php
$r = array('category__not_in'=&#62;array(22,23));
$r = wp_parse_args( $query_string, $r );
$ps = get_posts($r);
foreach($ps as $p) : ?&#62;
 &#60;p&#62;在这里加入你的处理代码，比如显示文章标题&#60;/p&#62;
 &#60;p&#62;&#60;?php echo get_the_title($p-&#62;ID); ?&#62;&#60;/p&#62;
&#60;?php endforeach; ?&#62;
这种方法的好处是比较独立，和默认的查询没有任何联系，但是不能调用the_title和the_content等这些模板函数。这里需要注意的是，get_posts的返回值千万不可传递给$posts,即不能这样写
$posts = get_posts($r);
因为这样会更改默认查询的posts值，从而引起混乱。如果你还想向以前那样直接使用the_title()和the_content()等模板函数的话，也不是不行，我前面也说过，你只需要修改$post这个值即可，即把foreach($ps as $p)改为foreach($ps as $post),完整代码如下
&#60;?php
$r [...]]]></description>
			<content:encoded><![CDATA[<p>如果说循环是wordpress模板系统的核心，那么查询就是循环的基础。没有查询得到的结果，循环就毫无意义，应该说循环根本就不会开始。但是查询出的结果又需要通过循环来把结果按照我们设计的格式显示出来。这就是两者之间的关系，今天就和大家介绍几种查询的方法。<span id="more-204"></span></p>
<h3>默认查询</h3>
<p>这种查询是自动发生的，几乎发生在我们的每次请求当中：当我们点击网站主页的时候、当我们点击某个分类的时候、当我们点击某个标签的时候&#8230;&#8230;默认的查询就已经开始执行，我们只需要实现循环，就可以把结果显示出来。</p>
<p>这个默认的查询保存在一个全局变量$wp_query之中，查询的文章结果保存在全局变量$posts之中（也可以说保存在$wp_query-&gt;posts中）。当我们执行循环的时候，当前文章保存在全局变量$post当中。你可以想一想，像the_title、the_content这样的函数，并没有传递参数，为什么还可以显示不同的文章？其实它们显示的就是$post中保存各个值，如果我们改变了$post，the_content显示的内容也会改变。所以，除非你很明确你要做的事情，不然不要随便改变$posts、$post。</p>
<h3>query_posts查询</h3>
<p>当我们不满意默认查询的结果，或者说想更改默认查询的时候，query_posts函数就派上了用场。query_posts的作用就是按照我特有的条件，更改默认的查询以返回我们想要的结果。下面举个简单的例子。</p>
<p>经常会碰到这样一种情况，我们不想在首页显示某个分类（或某几个分类）下的文章。可能我们首先想到的方法就是在循环中加上一个判断，如果判断是该分类下的文章就不显示。不错，这个方法是可行的，但是会有一个问题：假设你在后台设置了每页显示10篇文章，首页上有两篇你想过滤掉的分类文章。如果在循环中过滤的话，最终首页只会显示8篇文章，而不是10篇。</p>
<p>而用query_posts可以完美的解决这种问题，假设某个分类的ID为22，下面我们来看一下query_posts是如何从首页中排除该分类下的文章的。</p>
<p>在开始循环之前，加入以下代码</p>
<pre>$r = array('category__not_in'=&gt;array(22));
$r = wp_parse_args( $query_string, $r ); //$query_string是默认的查询字符串，如果没有这一句，分页将会失效
  query_posts($r);</pre>
<p>这样查询结果中就不包含分类ID为22的文章，如果还有其它分类不想显示（比如分类ID为23），只需要加入到category__not_in数组中即可，分类ID之间用逗号分开。以下给出完整的代码</p>
<pre>&lt;?php
$r = array('category__not_in'=&gt;array(22,23));
$r = wp_parse_args( $query_string, $r );
query_posts($r);
if(have_posts()) :
     while(have_posts()) : the_post();
?&gt;
      &lt;p&gt;在这里加入你的处理代码，比如显示文章标题&lt;/p&gt;
      &lt;p&gt;&lt;?php the_title(); ?&gt;&lt;/p&gt;
     &lt;?php endwhile; ?&gt;
&lt;?php endif; ?&gt;</pre>
<p>从代码中我们也可以看到，这种查询支持have_posts和the_posts函数，因为query_posts查询改变了默认的查询$wp_query。那么，如果我们再想利用默认查询怎么办呢？很简单，在上面的循环结束之后，调用wp_reset_query即可</p>
<pre>&lt;?php wp_reset_query(); ?&gt;</pre>
<h3>get_posts查询</h3>
<p>get_posts只返回查询结果中的posts，不会改变默认的查询，即不会修改$wp_query。也正是因为没有修改$wp_query,因此循环中不能使用hvae_posts和the_post函数。</p>
<p>使用get_posts查询，一般都是使用foreach循环。我们看一下上面的例子使用get_posts查询的实现方法</p>
<pre>&lt;?php
$r = array('category__not_in'=&gt;array(22,23));
$r = wp_parse_args( $query_string, $r );
$ps = get_posts($r);
foreach($ps as $p) : ?&gt;
 &lt;p&gt;在这里加入你的处理代码，比如显示文章标题&lt;/p&gt;
 &lt;p&gt;&lt;?php echo get_the_title($p-&gt;ID); ?&gt;&lt;/p&gt;
&lt;?php endforeach; ?&gt;</pre>
<p>这种方法的好处是比较独立，和默认的查询没有任何联系，但是不能调用the_title和the_content等这些模板函数。这里需要注意的是，get_posts的返回值千万不可传递给$posts,即不能这样写</p>
<pre>$posts = get_posts($r);</pre>
<p>因为这样会更改默认查询的posts值，从而引起混乱。如果你还想向以前那样直接使用the_title()和the_content()等模板函数的话，也不是不行，我前面也说过，你只需要修改$post这个值即可，即把foreach($ps as $p)改为foreach($ps as $post),完整代码如下</p>
<pre>&lt;?php
$r = array('category__not_in'=&gt;array(22,23));
$r = wp_parse_args( $query_string, $r );
$ps = get_posts($r);
foreach($ps as $post) : ?&gt;
 &lt;p&gt;在这里加入你的处理代码，比如显示文章标题&lt;/p&gt;
 &lt;p&gt;&lt;?php the_title(); ?&gt;&lt;/p&gt;
&lt;?php endforeach; ?&gt;</pre>
<p>不过需要注意一点，当循环结束之后，$post的值已经不是默认的值，因此在循环之后调用的所有和$post有关的函数，都会显示循环的最后一篇文章的结果，比如评论的个数会显示上次循环的最后一篇文章的评论个数。把$post恢复默认值的方法和上面的一样，在循环结束之后调用一下</p>
<pre>&lt;?php wp_reset_query(); ?&gt;</pre>
<p>即可。</p>
<h3>总结一下</h3>
<p>这三种查询应该是最常用的查询，还有一种更底层的查询，就是直接使用WP_Query类，其实效果和get_posts类似，在这里就不做解释了，有兴趣的可以发邮件给我一起讨论。在使用这些查询的过程中，要时刻注意不要轻易使用$wp_query、$posts、$post这些变量，除非你非常清楚它们的作用。(当然，还有一种最最底层的查询，直接通过sql语句查询。)</p>
<h3>附查询常用参数说明</h3>
<p>以下是可以直接通过字符串传递的参数</p>
<ul>
<li>showposts或posts_per_page —— 显示文章的数量（每页显示的数量），比如&#8217;showposts=10&#8242;</li>
<li>nopaging —— 不分页（即显示所有文章）,比如&#8217;nopaging=1&#8242;</li>
<li>m —— 日期和时间， 比如m=&#8217;20091231&#8242;</li>
<li>year —— 年份，比如&#8217;year=2009&#8242;</li>
<li>monthnum —— 月份，比如&#8217;monthnum=12&#8242;</li>
<li>day —— 日期，比如&#8217;day=31&#8242;</li>
<li>p —— 文章ID，比如&#8217;p=192&#8242;</li>
<li>cat —— 指定分类ID,多个id之间用逗号隔开。比如&#8217;cat=1,2,3&#8242;即查询分类ID为1，2，3下的文章；而&#8217;cat=-1,-2,-3&#8242;则表示查询不在分类ID为1，2，3的文章。</li>
<li>tag —— 指定标签名，多个标签名之间用逗号隔开，比如&#8217;tag=domety,wordpress&#8217;</li>
<li>orderby —— 排序，可以取值：&#8217;author&#8217;, &#8216;date&#8217;, &#8216;title&#8217;, &#8216;modified&#8217;, &#8216;menu_order&#8217;, &#8216;parent&#8217;, &#8216;ID&#8217;, &#8216;rand&#8217;, &#8216;comment_count&#8217;</li>
</ul>
<p>以下是需要通过数组传递的参数(以$r为例)</p>
<ul>
<li>post__in —— 查找指定的文章ID，比如$r['post__in'] = array(1,2,3,4,5);</li>
<li>post__not_int —— 需要排除的文章ID,比如$r['post__not_in'] = array(1,2,3,4,5);</li>
<li>cat__in —— 包含指定的分类ID，比如$r['cat__in'] = array(1,2,3);</li>
<li>cat__not_in —— 排除指定的分类ID，比如$r['cat__not_in'] = array(1,2,3);</li>
<li>tag__in —— 包含指定的标签ID</li>
<li>tag__not_in —— 排除指定的标签ID</li>
</ul>
<p>另外还有一些可以使用的参数，不过我还没有测试，等我测试以后再添加进来。</p>
<p>在本文的示例中使用的是数组传递参数的方式，也可以使用字符串传递的方式，比如</p>
<pre>query_posts($query_string.'&amp;cat=22,23');</pre>
<p>注意多个参数之间使用&amp;符号隔开。</p>
]]></content:encoded>
			<wfw:commentRss>http://domety.com/archives/204/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>wordpress循环之基础篇</title>
		<link>http://domety.com/archives/202/</link>
		<comments>http://domety.com/archives/202/#comments</comments>
		<pubDate>Wed, 30 Dec 2009 13:19:54 +0000</pubDate>
		<dc:creator>DDBug</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[The Loop]]></category>

		<guid isPermaLink="false">http://domety.com/?p=202</guid>
		<description><![CDATA[今天浏览了一下之前写过的wordpress文章，发现居然没有写wordpress模板中最核心也是最基础的东西：循环(the loop)。真是太失误了，掌握了wordpress循环，不仅对理解之前的文章有帮助，更重要的是对你设计主题以及修改主题都会起到很大的作用。从这一篇文章开始，我将对wordpress的循环机制做一个详细的介绍。
我们先从最基础的地方开始。在开始之前我想你应该对php有个了解或者对编程有点基础，比如知道程序的控制结构、变量等等。如果你对编程一窍不通，可能理解起来会有点困难，不过依葫芦画瓢也是可以的。
循环简介
在wordpress中，几乎每一个请求（比如首页的请求、查看某个分类下文章、查看某个标签下的文章、搜索等等），都会返回一系列的文章，通过循环对这些返回的文章进行遍历，可以把每一篇文章显示到屏幕上。
通过循环，你可以对文章进行有条件的过滤，或者针对不同的文章显示不同的样式
一个简单的循环
我们先写一个最简单的循环
&#60;?php
get_header();
if (have_posts()) :
   while (have_posts()) :
      the_post();
      the_title();
   endwhile;
endif;
get_footer();
?&#62;
就这么简单，如果这段代码写在index.php文件中，它就会显示出首页所有文章的标题(the_title()显示当前文章的标题)，首页文章的数量和你在后台控制面板定义的每页显示的文章数量有关，比如你定义了每页显示10篇文章，这里就会显示最新的10篇文章的标题。
默认主题中的循环
空说无凭，我们通过wordpress自带的默认主题，来看一下wordpress是怎么处理循环的。
打开default主题文件夹下的index.php文件，我们可以找到下面这样一段代码
 &#60;?php if (have_posts()) : ?&#62;
&#60;?php while (have_posts()) : the_post(); ?&#62;
这就是循环开始的地方，首先通过have_posts函数来判断当前的查询结果中是否包含文章，如果有文章就执行下面的while循环。while循环以have_posts为条件，当没有文章的时候（既已经遍历了所有的文章），就会跳出while循环。
在while循环内部，首先调用the_post函数，该函数的作用是把当前文章指向下一篇文章，每循环一次当前文章就会向前移动一步指向下一篇文章，这样才能保证整个循环的正常运转，直到当前文章指向了最后一篇文章，循环结束。可见，如果没有这个the_post函数，整个循环就是一个死循环，无限次的显示第一文章。
调用the_post之后，就可以通过调用wordpress的模板函数有选择的显示文章的内容，比如

文章标题 —— the_title()
文章链接 —— the_permalink()
文章发布时间 —— the_time()
文章内容 —— the_content()
文章分类 —— the_category()
文章标签 —— the_tages()

这些内容都是在while 和 endwhile 之间调用，所以接下来你可以看到
&#60;?php endwhile; ?&#62;
来结束循环。
另一种情况是查询结果中并没有任何文章，这个时候就会调用else之后的内容
&#60;?php else : ?&#62;

  &#60;h2&#62;&#60;?php _e('Not Found', 'kubrick'); ?&#62;&#60;/h2&#62;
  &#60;p&#62;&#60;?php _e('Sorry, but you are looking for something that isn&#38;#8217;t here.', [...]]]></description>
			<content:encoded><![CDATA[<p>今天浏览了一下之前写过的wordpress文章，发现居然没有写wordpress模板中最核心也是最基础的东西：循环(the loop)。真是太失误了，掌握了wordpress循环，不仅对理解之前的文章有帮助，更重要的是对你设计主题以及修改主题都会起到很大的作用。从这一篇文章开始，我将对wordpress的循环机制做一个详细的介绍。</p>
<p>我们先从最基础的地方开始。在开始之前我想你应该对php有个了解或者对编程有点基础，比如知道程序的控制结构、变量等等。如果你对编程一窍不通，可能理解起来会有点困难，不过依葫芦画瓢也是可以的。</p>
<h3>循环简介</h3>
<p>在wordpress中，几乎每一个请求（比如首页的请求、查看某个分类下文章、查看某个标签下的文章、搜索等等），都会返回一系列的文章，通过循环对这些返回的文章进行遍历，可以把每一篇文章显示到屏幕上。</p>
<p>通过循环，你可以对文章进行有条件的过滤，或者针对不同的文章显示不同的样式</p>
<h3>一个简单的循环</h3>
<p>我们先写一个最简单的循环</p>
<pre>&lt;?php
get_header();
if (have_posts()) :
   while (have_posts()) :
      the_post();
      the_title();
   endwhile;
endif;
get_footer();
?&gt;</pre>
<p>就这么简单，如果这段代码写在index.php文件中，它就会显示出首页所有文章的标题(the_title()显示当前文章的标题)，首页文章的数量和你在后台控制面板定义的每页显示的文章数量有关，比如你定义了每页显示10篇文章，这里就会显示最新的10篇文章的标题。</p>
<h3>默认主题中的循环</h3>
<p>空说无凭，我们通过wordpress自带的默认主题，来看一下wordpress是怎么处理循环的。</p>
<p>打开default主题文件夹下的index.php文件，我们可以找到下面这样一段代码</p>
<pre> &lt;?php if (have_posts()) : ?&gt;
&lt;?php while (have_posts()) : the_post(); ?&gt;</pre>
<p>这就是循环开始的地方，首先通过have_posts函数来判断当前的查询结果中是否包含文章，如果有文章就执行下面的while循环。while循环以have_posts为条件，当没有文章的时候（既已经遍历了所有的文章），就会跳出while循环。</p>
<p>在while循环内部，首先调用the_post函数，该函数的作用是把当前文章指向下一篇文章，每循环一次当前文章就会向前移动一步指向下一篇文章，这样才能保证整个循环的正常运转，直到当前文章指向了最后一篇文章，循环结束。可见，如果没有这个the_post函数，整个循环就是一个死循环，无限次的显示第一文章。</p>
<p>调用the_post之后，就可以通过调用wordpress的模板函数有选择的显示文章的内容，比如</p>
<ul>
<li>文章标题 —— the_title()</li>
<li>文章链接 —— the_permalink()</li>
<li>文章发布时间 —— the_time()</li>
<li>文章内容 —— the_content()</li>
<li>文章分类 —— the_category()</li>
<li>文章标签 —— the_tages()</li>
</ul>
<p>这些内容都是在while 和 endwhile 之间调用，所以接下来你可以看到</p>
<pre>&lt;?php endwhile; ?&gt;</pre>
<p>来结束循环。</p>
<p>另一种情况是查询结果中并没有任何文章，这个时候就会调用else之后的内容</p>
<pre>&lt;?php else : ?&gt;

  &lt;h2&gt;&lt;?php _e('Not Found', 'kubrick'); ?&gt;&lt;/h2&gt;
  &lt;p&gt;&lt;?php _e('Sorry, but you are looking for something that isn&amp;#8217;t here.', 'kubrick'); ?&gt;&lt;/p&gt;
  &lt;?php get_search_form(); ?&gt;</pre>
<p>最后是一条结束if判断的语句</p>
<pre> &lt;?php endif; ?&gt;</pre>
<p>这就是整个循环的过程，你可以在大部分的主题中看到这样的结构，为了更清楚的说明整个循环流程，我画了一张图</p>
<p><img class="alignnone" title="The Lopp" src="http://i623.domety.com/albums/tt312/baolai5/200912/The_Loop.png" alt="" width="586" height="558" /></p>
<p>希望可以帮助理解。</p>
]]></content:encoded>
			<wfw:commentRss>http://domety.com/archives/202/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Wordpress中为你的文章添加“相关文章”</title>
		<link>http://domety.com/archives/146/</link>
		<comments>http://domety.com/archives/146/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 08:30:35 +0000</pubDate>
		<dc:creator>DDBug</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[The Loop]]></category>

		<guid isPermaLink="false">http://domety.com/?p=146</guid>
		<description><![CDATA[前几天向大家介绍了如何实现“随机文章”的方法，今天将要介绍的这个“相关文章”的方法其实是差不多的，在随机文章的基础上稍加修改即可。
首先我们看一下什么样的文章才算是“相关文章”，对“相关文章”这个词的理解，不同人的理解可能会有点出入。我是这样认为的：具有相同标签的文章相关性最强，然后是具有相同分类的分章。
根据上面我对“相关文章”的理解，查找“相关文章”的过程如下：


首先随机显示和文章具有相同标签的文章
如果找到的文章数量不足指定的数量，则继续随机显示和文章分类相同的文章
如果还是不足指定的文章数量，则随机在全站显示几篇文章

关键代码分析
由于随机显示的工作比较多，我们首先来定义一个可以根据分类ID或者标签名显示随机文章的函数the_rand_posts，有关the_rand_posts函数，可以参看WordPress添加“随机文章”模块 一文。下面是升级版本的the_rand_posts函数
function the_rand_posts($args = '',$post_not_in = array()){

 global $post;

 $default = array('showposts'=&#62;10,
  'cat'=&#62;'',
  'tag'=&#62;'',
  'class'=&#62;'randomPosts',
  'useul'=&#62;1);

 $r = wp_parse_args($args,$default);

 extract($r);

 $r['orderby'] = 'rand';
 $r['post__not_in'] = $post_not_in;
 $rand_query = new WP_Query($r);

 $output = '';
 $post_ids = array();

 foreach($rand_query-&#62;posts as $p){
  $post_ids[] = $p-&#62;ID;
  $output .= '&#60;li&#62;&#60;a href="'.get_permalink($p).'"&#62;'.get_the_title($p-&#62;ID).'&#60;/a&#62;&#60;/li&#62;';
 }

 if($useul)
  $output = '&#60;ul&#62;' .$output . '&#60;/ul&#62;';
 echo $output;

 return $post_ids;
}
其中showposts指定显示的文章数量，cat指定查找的分类ID范围，tag指定查找的标签名范围，useul决定是否使用&#60;ul&#62;&#60;/ul&#62;标签。该函数返回查找到的文章ID数组。
下面看一下如何根据文章标签查找指定数量的相关文章：
$terms = get_the_tags();
 foreach($terms as $term){
  $tags[] = $term-&#62;name;
 }
这段代码的作用是获取文章的标签名，保存到$tags数组中
接下来把$tags数组转换为用&#8217;，&#8217;号隔开的字符串，并传递给the_rand_posts来随机显示
$tag = implode(",",$tags);
  $post_not_in[] = $post-&#62;ID;
  $post_ids = the_rand_posts("useul=0&#38;showposts=$showposts&#38;tag=$tag",$post_not_in);
如果知道查找到的文章的数量呢，通过计算$post_ids数组的长度即可。$post_not_in是用来避免查找复制的文章。如果文章数量不足我们指定的$showposts，则再根据文章分类ID继续查找
$post_not_in = array_merge($post_not_in,$post_ids);
  $count = count($post_ids);
 if($count &#60; $showposts){
  $showposts -= [...]]]></description>
			<content:encoded><![CDATA[<p>前几天向大家介绍了如何实现“随机文章”的方法，今天将要介绍的这个“相关文章”的方法其实是差不多的，在随机文章的基础上稍加修改即可。</p>
<p>首先我们看一下什么样的文章才算是“相关文章”，对“相关文章”这个词的理解，不同人的理解可能会有点出入。我是这样认为的：具有相同标签的文章相关性最强，然后是具有相同分类的分章。</p>
<p>根据上面我对“相关文章”的理解，查找“相关文章”的过程如下：</p>
<p><span id="more-146"></span></p>
<ol>
<li>首先随机显示和文章具有相同标签的文章</li>
<li>如果找到的文章数量不足指定的数量，则继续随机显示和文章分类相同的文章</li>
<li>如果还是不足指定的文章数量，则随机在全站显示几篇文章</li>
</ol>
<h3>关键代码分析</h3>
<p>由于随机显示的工作比较多，我们首先来定义一个可以根据分类ID或者标签名显示随机文章的函数the_rand_posts，有关the_rand_posts函数，可以参看<a title="Permalink to WordPress添加“随机文章”模块" href="http://domety.com/archives/139">WordPress添加“随机文章”模块</a> 一文。下面是升级版本的the_rand_posts函数</p>
<pre>function the_rand_posts($args = '',$post_not_in = array()){

 global $post;

 $default = array('showposts'=&gt;10,
  'cat'=&gt;'',
  'tag'=&gt;'',
  'class'=&gt;'randomPosts',
  'useul'=&gt;1);

 $r = wp_parse_args($args,$default);

 extract($r);

 $r['orderby'] = 'rand';
 $r['post__not_in'] = $post_not_in;
 $rand_query = new WP_Query($r);

 $output = '';
 $post_ids = array();

 foreach($rand_query-&gt;posts as $p){
  $post_ids[] = $p-&gt;ID;
  $output .= '&lt;li&gt;&lt;a href="'.get_permalink($p).'"&gt;'.get_the_title($p-&gt;ID).'&lt;/a&gt;&lt;/li&gt;';
 }

 if($useul)
  $output = '&lt;ul&gt;' .$output . '&lt;/ul&gt;';
 echo $output;

 return $post_ids;
}</pre>
<p>其中showposts指定显示的文章数量，cat指定查找的分类ID范围，tag指定查找的标签名范围，useul决定是否使用&lt;ul&gt;&lt;/ul&gt;标签。该函数返回查找到的文章ID数组。</p>
<p>下面看一下如何根据文章标签查找指定数量的相关文章：</p>
<pre>$terms = get_the_tags();
 foreach($terms as $term){
  $tags[] = $term-&gt;name;
 }</pre>
<p>这段代码的作用是获取文章的标签名，保存到$tags数组中</p>
<p>接下来把$tags数组转换为用&#8217;，&#8217;号隔开的字符串，并传递给the_rand_posts来随机显示</p>
<pre>$tag = implode(",",$tags);
  $post_not_in[] = $post-&gt;ID;
  $post_ids = the_rand_posts("useul=0&amp;showposts=$showposts&amp;tag=$tag",$post_not_in);</pre>
<p>如果知道查找到的文章的数量呢，通过计算$post_ids数组的长度即可。$post_not_in是用来避免查找复制的文章。如果文章数量不足我们指定的$showposts，则再根据文章分类ID继续查找</p>
<pre>$post_not_in = array_merge($post_not_in,$post_ids);
  $count = count($post_ids);
 if($count &lt; $showposts){
  $showposts -= $count;
  $categorys = get_the_category();
  foreach($categorys as $category){
   $cats[] = $category-&gt;term_id;
  }
  $cat = implode(",",$cats);
  $post_ids = the_rand_posts("useul=0&amp;showposts=$showposts&amp;cat=$cat",$post_not_in);

}</pre>
<p>该段代码中通过调用get_the_category函数来获取文章的分类信息，并通过一个循环来保存分类ID列表，然后传递给the_rand_posts函数进行查找并随机显示。</p>
<p>最后我们再做一次判断，如果显示的文章数量仍然不足我们指定的文章数量，就从全站的文章中随机显示几篇</p>
<pre>$count = count($post_ids);
  $post_not_in = array_merge($post_not_in,$post_ids);
 if($count &lt; $showposts){
  $showposts -= $count;
  the_rand_posts("useul=0&amp;showposts=$showposts",$post_not_in);
 }</pre>
<h3>使用方法</h3>
<p>完整代码如下：</p>
<pre>function the_rand_posts($args = '',$post_not_in = array()){

 global $post;

 $default = array('showposts'=&gt;10,
  'cat'=&gt;'',
  'tag'=&gt;'',
  'class'=&gt;'randomPosts',
  'useul'=&gt;1);

 $r = wp_parse_args($args,$default);

 extract($r);

 $r['orderby'] = 'rand';
 $r['post__not_in'] = $post_not_in;
 $rand_query = new WP_Query($r);

 $output = '';
 $post_ids = array();

 foreach($rand_query-&gt;posts as $p){
  $post_ids[] = $p-&gt;ID;
  $output .= '&lt;li&gt;&lt;a href="'.get_permalink($p).'"&gt;'.get_the_title($p-&gt;ID).'&lt;/a&gt;&lt;/li&gt;';
 }

 if($useul)
  $output = '&lt;ul&gt;' .$output . '&lt;/ul&gt;';
 echo $output;

 return $post_ids;
}
function the_related_posts($args = ''){
 global $post;

 $default = array('showposts'=&gt;10,
  'class'=&gt;'relatedPosts');
 $r = wp_parse_args($args,$default);
 extract($r);

 echo '&lt;ul&gt;';

 $tags = array();
 $terms = get_the_tags();
 foreach($terms as $term){
  $tags[] = $term-&gt;name;
 }
 if(!empty($tags)){
  $tag = implode(",",$tags);
  $post_not_in[] = $post-&gt;ID;
  $post_ids = the_rand_posts("useul=0&amp;showposts=$showposts&amp;tag=$tag",$post_not_in);
  $post_not_in = array_merge($post_not_in,$post_ids);
  $count = count($post_ids);
 }
 if($count &lt; $showposts){
  $showposts -= $count;
  $categorys = get_the_category();
  foreach($categorys as $category){
   $cats[] = $category-&gt;term_id;
  }
  $cat = implode(",",$cats);
  $post_ids = the_rand_posts("useul=0&amp;showposts=$showposts&amp;cat=$cat",$post_not_in);
  $count = count($post_ids);
  $post_not_in = array_merge($post_not_in,$post_ids);
 }
 if($count &lt; $showposts){
  $showposts -= $count;
  the_rand_posts("useul=0&amp;showposts=$showposts",$post_not_in);
 }
 echo '&lt;/ul&gt;';
}</pre>
<p>如果你懒得分析代码，可以把以上完整的代码复制到你正在使用主题文件夹下的functions.php文件中，然后在single.php文件中调用</p>
<pre>&lt;?php if(function_exists('the_related_posts')) {the_related_posts();} ?&gt;</pre>
<p>或者继续关注本站，不日将发布&#8221;随机文章&#8221;插件的升级版本，集成“相关文章”功能。</p>
<p>The End</p>
]]></content:encoded>
			<wfw:commentRss>http://domety.com/archives/146/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>WordPress添加“随机文章”模块</title>
		<link>http://domety.com/archives/139/</link>
		<comments>http://domety.com/archives/139/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 15:30:51 +0000</pubDate>
		<dc:creator>DDBug</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[The Loop]]></category>

		<guid isPermaLink="false">http://domety.com/?p=139</guid>
		<description><![CDATA[自从wordpress 2.5 之后，wordpress增加了一些功能，其中就包括显示随机文章，使用起来也很方便，看代码

&#60;ul&#62;
&#60;?php query_posts('showposts=5&#38;orderby=rand'); ?&#62;
&#60;?php if (have_posts()) : while (have_posts()) : the_post(); ?&#62;
&#60;li&#62;
&#60;a href="&#60;?php the_permalink(); ?&#62;"&#62;&#60;?php the_title(); ?&#62;&#60;/a&#62;
&#60;/li&#62;
&#60;?php endwhile; endif; ?&#62;
&#60;/ul&#62;

这段代码的功能就是以列表的形式随机显示5篇文章的标题。其实就是调用了query_posts()函数来自定义我们的查询语句，其中showposts参数决定文章数量，orderby决定显示顺序，我们让orderby=rand就可以实现随机的效果。

使用这种方法有些问题需要注意，query_posts覆盖了原始的查询语句，不能把这段代码插入到其它的循环中。如果你对wordpress的循环机制不是很了解，使用这种方法就需要谨慎。在进行多循环查询的时候，这种方法可能会影响到其它查询。
为了解决可能对其它循环体的影响，我们可以在查询随机文章之前先备份一下原始原始查询语句，然后再复原，代码如下：
&#60;ul&#62;
&#60;?php $temp_query = clone $wp_query; ?&#62;
&#60;?php query_posts('showposts=5&#38;orderby=rand'); ?&#62;
&#60;?php if (have_posts()) : while (have_posts()) : the_post(); ?&#62;
&#60;li&#62;
&#60;a href="&#60;?php the_permalink(); ?&#62;"&#62;&#60;?php the_title(); ?&#62;&#60;/a&#62;
&#60;/li&#62;
&#60;?php endwhile; endif; ?&#62;
&#60;?php $wp_query = clone $temp_query; ?&#62;
&#60;/ul&#62;
在这段代码中，我们首先把原始的查询变量$wp_query复制给临时变量$temp_query。当我们在进行随机查询的时候，全局变量$wp_query已经改变。在循环结束之后，再把之前保存的$temp_query复制给$wp_query，此时$wp_query就仍然是先前的原始查询语句了。
另外一种方法是新建一个查询变量，这样就不会覆盖原始的查询变量$wp_query。具体代码如下：
&#60;ul&#62;
&#60;?php $rand_query = new WP_Query('showposts=5&#38;orderby=rand'); ?&#62;
&#60;?php if [...]]]></description>
			<content:encoded><![CDATA[<p>自从wordpress 2.5 之后，wordpress增加了一些功能，其中就包括显示随机文章，使用起来也很方便，看代码</p>
<pre>
<pre>&lt;ul&gt;
&lt;?php query_posts('showposts=5&amp;orderby=rand'); ?&gt;
&lt;?php if (have_posts()) : while (have_posts()) : the_post(); ?&gt;
&lt;li&gt;
&lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;?php endwhile; endif; ?&gt;
&lt;/ul&gt;</pre>
</pre>
<p>这段代码的功能就是以列表的形式随机显示5篇文章的标题。其实就是调用了query_posts()函数来自定义我们的查询语句，其中showposts参数决定文章数量，orderby决定显示顺序，我们让orderby=rand就可以实现随机的效果。</p>
<p><span id="more-139"></span></p>
<p>使用这种方法有些问题需要注意，query_posts覆盖了原始的查询语句，不能把这段代码插入到其它的循环中。如果你对wordpress的循环机制不是很了解，使用这种方法就需要谨慎。在进行多循环查询的时候，这种方法可能会影响到其它查询。</p>
<p>为了解决可能对其它循环体的影响，我们可以在查询随机文章之前先备份一下原始原始查询语句，然后再复原，代码如下：</p>
<pre>&lt;ul&gt;
&lt;?php $temp_query = clone $wp_query; ?&gt;
&lt;?php query_posts('showposts=5&amp;orderby=rand'); ?&gt;
&lt;?php if (have_posts()) : while (have_posts()) : the_post(); ?&gt;
&lt;li&gt;
&lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;?php endwhile; endif; ?&gt;
&lt;?php $wp_query = clone $temp_query; ?&gt;
&lt;/ul&gt;</pre>
<p>在这段代码中，我们首先把原始的查询变量$wp_query复制给临时变量$temp_query。当我们在进行随机查询的时候，全局变量$wp_query已经改变。在循环结束之后，再把之前保存的$temp_query复制给$wp_query，此时$wp_query就仍然是先前的原始查询语句了。</p>
<p>另外一种方法是新建一个查询变量，这样就不会覆盖原始的查询变量$wp_query。具体代码如下：</p>
<pre>&lt;ul&gt;
&lt;?php $rand_query = new WP_Query('showposts=5&amp;orderby=rand'); ?&gt;
&lt;?php if ($rand_query-&gt;have_posts()) : while ($rand_query-&gt;have_posts()) : $rand_query-&gt;the_post(); ?&gt;
&lt;li&gt;
&lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;?php endwhile; endif; ?&gt;
&lt;/ul&gt;</pre>
<p>在这段代码中，我们首先定义了一个新的查询变量$rand_query,之后的循环语句也是对$rand_query的操作，这样就不会影响到全局变量$wp_query。</p>
<p>我个人比较倾向于最后这种方法，因为这段代码是完全独立的，你可以把它放在任何你需要的地方，不会对其它循环产生影响。</p>
<p>为了方便大家使用，我写了一个函数：</p>
<pre>function the_rand_posts($postsNum = 10){
 $rand_query = new WP_Query("showposts=$postsNum&amp;orderby=rand");
 if($rand_query-&gt;have_posts()){
  echo '&lt;ul&gt;';
  while($rand_query-&gt;have_posts()){
   $rand_query-&gt;the_post();
   echo '&lt;li&gt;&lt;a href="'.get_permalink().'"&gt;'.get_the_title().'&lt;/a&gt;&lt;/li&gt;';
  }/*End while($rand_query-&gt;have_posts())*/
  echo '&lt;/ul&gt;';
 }/*End if($rand_query-&gt;have_posts())*/
}</pre>
<p>你只需要把这段代码复制到你正在使用的主题文件夹下的functions.php中，然后在任何你需要显示随机文章的地方加入下面这句代码即可：</p>
<pre>&lt;?php the_rand_posts(); ?&gt;</pre>
<p>这样就会显示10篇随机文章标题，如果你只想显示5篇，可以这样调用：</p>
<pre>&lt;?php the_rand_posts(5); ?&gt;</pre>
<p>好了，今天DDBug就为你介绍到这里，如果有任何疑问，可以留言。同时欢迎各方英雄来讨论相关话题。</p>
]]></content:encoded>
			<wfw:commentRss>http://domety.com/archives/139/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

