<?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>Life Studio &#187; 循环</title>
	<atom:link href="http://wange.im/tag/loop/feed" rel="self" type="application/rss+xml" />
	<link>http://wange.im</link>
	<description>My Life, My Studio...</description>
	<lastBuildDate>Sat, 05 May 2012 00:26:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>WordPress 分离并调用第一篇最新日志</title>
		<link>http://wange.im/wordpress-isolated-latest-post.html</link>
		<comments>http://wange.im/wordpress-isolated-latest-post.html#comments</comments>
		<pubDate>Mon, 30 May 2011 14:08:31 +0000</pubDate>
		<dc:creator>万戈</dc:creator>
				<category><![CDATA[电脑网络]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[循环]]></category>

		<guid isPermaLink="false">http://wange.im/?p=4895</guid>
		<description><![CDATA[这个标题真是让我难以表达，估计一大半的人都没理解这标题的意思。大家看我的首页就可以更好地理解本文要实现的 WordPress 功能和效果，很多时候，为了强调突出第一篇最新日志，我们会将它单独循环调用出来显示。这样做的好处很明显，可以把显要的位置让给第一篇最新日志，吸引更多的点击量，同时也要做好心理准备，因为这将在同一个页面上多产生一次主循环。 在 WordPress 的主循环中，我们一般都是这样开始的： &#60;?php if (have_posts()):while (have_posts()):the_post(); ?&#62; 要分离循环出第一篇最新日志，我们就需要把它改成这样： &#60;?php $my_query = new WP_Query('showposts=1'); while ($my_query-&#62;have_posts()) : $my_query-&#62;the_post(); $do_not_duplicate = $post-&#62;ID; ?&#62; 已经分离显示出 WordPress 的第一篇最新日志，在接下来的主循环中，我们就要将这第一篇给排除掉，要排除的这篇文章 ID 我们已经赋给它 $do_not_duplicate 的这个变量，这样就好办了： &#60;?php if (have_posts()):while (have_posts()):the_post(); if( $post-&#62;ID == $do_not_duplicate ) continue; update_post_caches($posts); ?&#62; 这个方法在我之前的一个主题就有用到，现在一直延用，但是方法一直没有写过，最近又有童鞋问起，就正好凑篇文章给写了，不过这个方法除了会多引一次循环外，还会产生一个问题，就是分页。比如说，我现在设置了每页显示12篇文章，看我的首页，第一篇单独调用的日志加上下面的文章列表确实一共是12篇，但是翻到第2页就不对了，第一篇单独调用的日志加上下面的文章列表就有13篇了，也就是说除了首页，其他分页页面都不把第一篇文章作为分页内容显示。 方法还是挺简单的，但是还有更多扩展的用法，各位自己探索吧。 与 WordPress,循环 相关的文章 在 Wordpress 循环中排除某分类 2010年01月10日 万戈牌 WordPress [...]]]></description>
			<content:encoded><![CDATA[<p>这个标题真是让我难以表达，估计一大半的人都没理解这标题的意思。大家看我的<a title="Life Sutdio" href="http://wange.im">首页</a>就可以更好地理解本文要实现的 WordPress 功能和效果，很多时候，为了强调突出第一篇最新日志，我们会将它单独循环调用出来显示。这样做的好处很明显，可以把显要的位置让给第一篇最新日志，吸引更多的点击量，同时也要做好心理准备，因为这将在同一个页面上多产生一次主循环。</p>
<p>在 WordPress 的主循环中，我们一般都是这样开始的：</p>
<div class="source" style="font-family: &quot;Courier New&quot;,&quot;Consolas&quot;,&quot;Lucida Console&quot;; color: #000000;"><span style="color: #008080;">&lt;?php</span><br />
<span style="color: #000080; font-weight: bold;">if</span> (<span style="color: #000000;">have_posts</span>())<span style="color: #000000;">:</span><span style="color: #000080; font-weight: bold;">while</span> (<span style="color: #000000;">have_posts</span>())<span style="color: #000000;">:</span><span style="color: #000000;">the_post</span>();<br />
<span style="color: #008080;">?&gt;</span></div>
<p>要分离循环出第一篇最新日志，我们就需要把它改成这样：</p>
<div class="source" style="font-family: &quot;Courier New&quot;,&quot;Consolas&quot;,&quot;Lucida Console&quot;; color: #000000;"><span style="color: #008080;">&lt;?php</span><br />
<span style="color: #000000;">$my_query</span> <span style="color: #000000;">=</span> <span style="color: #000080; font-weight: bold;">new</span> <span style="color: #000000;">WP_Query</span>(<span style="color: #0000ff;">'showposts=1'</span>);<br />
<span style="color: #000080; font-weight: bold;">while</span> (<span style="color: #000000;">$my_query</span><span style="color: #000000;">-&gt;</span><span style="color: #ff0000;">have_posts</span>()) <span style="color: #000000;">:</span> <span style="color: #000000;">$my_query</span><span style="color: #000000;">-&gt;</span><span style="color: #ff0000;">the_post</span>();<br />
<span style="color: #000000;">$do_not_duplicate</span> <span style="color: #000000;">=</span> <span style="color: #000000;">$post</span><span style="color: #000000;">-&gt;</span><span style="color: #ff0000;">ID</span>;<br />
<span style="color: #008080;">?&gt;</span></div>
<p>已经分离显示出 WordPress 的第一篇最新日志，在接下来的主循环中，我们就要将这第一篇给排除掉，要排除的这篇文章 ID 我们已经赋给它 $do_not_duplicate 的这个变量，这样就好办了：</p>
<div class="source" style="font-family: &quot;Courier New&quot;,&quot;Consolas&quot;,&quot;Lucida Console&quot;; color: #000000;"><span style="color: #008080;">&lt;?php</span><br />
<span style="color: #000080; font-weight: bold;">if</span> (<span style="color: #000000;">have_posts</span>())<span style="color: #000000;">:</span><span style="color: #000080; font-weight: bold;">while</span> (<span style="color: #000000;">have_posts</span>())<span style="color: #000000;">:</span><span style="color: #000000;">the_post</span>();<br />
<span style="color: #000080; font-weight: bold;">if</span>( <span style="color: #000000;">$post</span><span style="color: #000000;">-&gt;</span><span style="color: #ff0000;">ID</span> <span style="color: #000000;">==</span> <span style="color: #000000;">$do_not_duplicate</span> ) <span style="color: #000080; font-weight: bold;">continue</span>;<br />
<span style="color: #000000;">update_post_caches</span>(<span style="color: #000000;">$posts</span>);<br />
<span style="color: #008080;">?&gt;</span></div>
<p>这个方法在我之前的一个主题就有用到，现在一直延用，但是方法一直没有写过，最近又有童鞋问起，就正好凑篇文章给写了，不过这个方法除了会多引一次循环外，还会产生一个问题，就是分页。比如说，我现在设置了每页显示12篇文章，看我的首页，第一篇单独调用的日志加上下面的文章列表确实一共是12篇，但是翻到第2页就不对了，第一篇单独调用的日志加上下面的文章列表就有13篇了，也就是说除了首页，其他分页页面都不把第一篇文章作为分页内容显示。</p>
<p>方法还是挺简单的，但是还有更多扩展的用法，各位自己探索吧。</p>
<div id="rl_posts">    <div id="rl_posts_hd" class="box_hd">        <span id="rl_posts_title" class="box_title">与 <h2 itemprop="keywords"><a href="http://wange.im/tag/wordpress/" title="WordPress" rel="tag">WordPress</a>,<a href="http://wange.im/tag/loop/" title="循环" rel="tag">循环</a></h2> 相关的文章        </span>    </div>    <div id="rl_posts_bd">        <ul class="clearfix"><li>    <div class="rl_thumb"><img src="http://wange.im/wp-content/themes/wange/imgs/category/computer.jpg" alt="电脑网络" width="45" height="45" itemprop="thumbnailUrl" />    </div>    <div class="rl_title">        <a href="http://wange.im/wordpress-exclude-category.html" rel="bookmark" title="在 Wordpress 循环中排除某分类">在 Wordpress 循环中排除某分类</a>        <div class="rl_date">2010年01月10日</div>    </div></li><li>    <div class="rl_thumb"><img src="http://wange.im/wp-content/themes/wange/timthumb.php?src=http://wange.im/cache/wg-tags-tip.jpg&amp;h=45&amp;w=45&amp;zc=1" alt="万戈牌 WordPress 搜索自动匹配提示标签关键词" width="45" height="45" itemprop="thumbnailUrl" />    </div>    <div class="rl_title">        <a href="http://wange.im/wg-tags-tip.html" rel="bookmark" title="万戈牌 WordPress 搜索自动匹配提示标签关键词">万戈牌 WordPress 搜索自动匹配提示标签关键词</a>        <div class="rl_date">2012年04月7日</div>    </div></li><li>    <div class="rl_thumb"><img src="http://wange.im/wp-content/themes/wange/timthumb.php?src=http://wange.im/cache/screenshot.jpg&amp;h=45&amp;w=45&amp;zc=1" alt="新年新气象，博客换新衣" width="45" height="45" itemprop="thumbnailUrl" />    </div>    <div class="rl_title">        <a href="http://wange.im/new-year-new-theme.html" rel="bookmark" title="新年新气象，博客换新衣">新年新气象，博客换新衣</a>        <div class="rl_date">2012年01月20日</div>    </div></li><li>    <div class="rl_thumb"><img src="http://wange.im/wp-content/themes/wange/timthumb.php?src=http://wange.im/cache/2011-12-29_194903.jpg&amp;h=45&amp;w=45&amp;zc=1" alt="2011 年终总结" width="45" height="45" itemprop="thumbnailUrl" />    </div>    <div class="rl_title">        <a href="http://wange.im/2011-year-end-summary.html" rel="bookmark" title="2011 年终总结">2011 年终总结</a>        <div class="rl_date">2011年12月29日</div>    </div></li>        </ul>    </div></div>]]></content:encoded>
			<wfw:commentRss>http://wange.im/wordpress-isolated-latest-post.html/feed</wfw:commentRss>
		<slash:comments>123</slash:comments>
		</item>
		<item>
		<title>在 WordPress 循环中排除某分类</title>
		<link>http://wange.im/wordpress-exclude-category.html</link>
		<comments>http://wange.im/wordpress-exclude-category.html#comments</comments>
		<pubDate>Sun, 10 Jan 2010 07:24:45 +0000</pubDate>
		<dc:creator>万戈</dc:creator>
				<category><![CDATA[电脑网络]]></category>
		<category><![CDATA[转来载去]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[循环]]></category>

		<guid isPermaLink="false">http://wange.im/?p=2530</guid>
		<description><![CDATA[最近正打算要改版当前主题，不只是外观上，还有在内容上，不过在技术上还有些小问题，每次折腾 WordPress 都会有收获，这次也是，已经解决了一些 WordPress 的问题，老规矩，写下来当笔记。 一、Wordpress 主循环外排除特定分类： 用途举例：一般 WordPress 主题的 index.php 都是输出所有分类的日志，但是对于不同的需求可以 DIY 一下，比如说你想隐藏某特定分类下的日志，让其不在首页显示，但是这又不同于私有日志，它是公开可见的。这时候就需要在 the loop 之外将此特定分类排除。 在 WordPress 默认模板中，主循环调用文章的代码格式如下： &#60;?php if (have_posts()) : ?&#62; &#60;?php while (have_posts()) : the_post(); ?&#62; //主循环内容 &#60;?php endwhile; ?&#62; &#60;?php endif; ?&#62; 默认情况下，文章调用范围为所有分类。如果排除某分类，可以使用 query_posts() 函数： &#60;?php query_posts('cat=-1'); ?&#62; 以上代码调用除分类 ID 为 1 外的所有文章。将其插入主循环前即可。 由于 query_posts() 函数会与分页功能造成冲突，可以将 query_posts() 语句替换为： &#60;?php [...]]]></description>
			<content:encoded><![CDATA[<p>最近正打算要改版当前主题，不只是外观上，还有在内容上，不过在技术上还有些小问题，每次折腾 WordPress 都会有收获，这次也是，已经解决了一些 WordPress 的问题，老规矩，写下来当笔记。</p>
<p><strong>一、Wordpress 主循环外排除特定分类：</strong></p>
<p>用途举例：一般 WordPress 主题的 index.php 都是输出所有分类的日志，但是对于不同的需求可以 DIY 一下，比如说你想隐藏某特定分类下的日志，让其不在首页显示，但是这又不同于私有日志，它是公开可见的。这时候就需要在 the loop 之外将此特定分类排除。</p>
<p>在 WordPress 默认模板中，主循环调用文章的代码格式如下：</p>
<div class="source" style="font-family: 'Courier New','Lucida Console','monospace'; color: #000000;"><span style="color: #008080;">&lt;?php</span> <span style="color: #000080; font-weight: bold;">if</span> (<span style="color: #000000;">have_posts</span>()) <span style="color: #000000;">:</span> <span style="color: #008080;">?&gt;</span><br />
<span style="color: #008080;">&lt;?php</span> <span style="color: #000080; font-weight: bold;">while</span> (<span style="color: #000000;">have_posts</span>()) <span style="color: #000000;">:</span> <span style="color: #000000;">the_post</span>(); <span style="color: #008080;">?&gt;</span><br />
<span style="color: #000000;">//主循环内容</span><br />
<span style="color: #008080;">&lt;?php</span> <span style="color: #000080; font-weight: bold;">endwhile</span>; <span style="color: #008080;">?&gt;</span><br />
<span style="color: #008080;">&lt;?php</span> <span style="color: #000080; font-weight: bold;">endif</span>; <span style="color: #008080;">?&gt;</span></div>
<p>默认情况下，文章调用范围为所有分类。如果排除某分类，可以使用 query_posts() 函数：</p>
<div class="source" style="font-family: 'Courier New','Lucida Console','monospace'; color: #000000;"><span style="color: #008080;">&lt;?php</span> <span style="color: #000000;">query_posts</span>(<span style="color: #0000ff;">'cat=-1'</span>); <span style="color: #008080;">?&gt;</span></div>
<p>以上代码调用除分类 ID 为 1 外的所有文章。将其插入主循环前即可。</p>
<p>由于 query_posts() 函数会与分页功能造成冲突，可以将 query_posts() 语句替换为：</p>
<div class="source" style="font-family: 'Courier New','Lucida Console','monospace'; color: #000000;"><span style="color: #008080;">&lt;?php</span><br />
<span style="color: #000000;">$limit</span> <span style="color: #000000;">=</span> <span style="color: #000000;">get_option</span>(<span style="color: #0000ff;">'posts_per_page'</span>);<br />
<span style="color: #000000;">$paged</span> <span style="color: #000000;">=</span> (<span style="color: #000000;">get_query_var</span>(<span style="color: #0000ff;">'paged'</span>)) <span style="color: #000000;">?</span> <span style="color: #000000;">get_query_var</span>(<span style="color: #0000ff;">'paged'</span>) <span style="color: #000000;">:</span> <span style="color: #0000ff;">1</span>;<br />
<span style="color: #000000;">query_posts</span>(<span style="color: #0000ff;">'cat=-1&amp;showposts='</span> <span style="color: #000000;">.</span> <span style="color: #000000;">$limit</span><span style="color: #000000;">=</span><span style="color: #0000ff;">10</span> <span style="color: #000000;">.</span> <span style="color: #0000ff;">'&amp;paged='</span> <span style="color: #000000;">.</span> <span style="color: #000000;">$paged</span>);<br />
<span style="color: #008080;">?&gt;</span></div>
<p>若需排除特定分类，又要保持分页功能正常，则完整代码如下：</p>
<div class="source" style="font-family: 'Courier New','Lucida Console','monospace'; color: #000000;"><span style="color: #008080;">&lt;?php</span><br />
<span style="color: #000000;">$limit</span> <span style="color: #000000;">=</span> <span style="color: #000000;">get_option</span>(<span style="color: #0000ff;">'posts_per_page'</span>);<br />
<span style="color: #000000;">$paged</span> <span style="color: #000000;">=</span> (<span style="color: #000000;">get_query_var</span>(<span style="color: #0000ff;">'paged'</span>)) <span style="color: #000000;">?</span> <span style="color: #000000;">get_query_var</span>(<span style="color: #0000ff;">'paged'</span>) <span style="color: #000000;">:</span> <span style="color: #0000ff;">1</span>;<br />
<span style="color: #000000;">query_posts</span>(<span style="color: #0000ff;">'cat=-1&amp;showposts='</span> <span style="color: #000000;">.</span> <span style="color: #000000;">$limit</span><span style="color: #000000;">=</span><span style="color: #0000ff;">10</span> <span style="color: #000000;">.</span> <span style="color: #0000ff;">'&amp;paged='</span> <span style="color: #000000;">.</span> <span style="color: #000000;">$paged</span>);<br />
<span style="color: #008080;">?&gt;</span><br />
<span style="color: #008080;">&lt;?php</span> <span style="color: #000080; font-weight: bold;">if</span> (<span style="color: #000000;">have_posts</span>()) <span style="color: #000000;">:</span> <span style="color: #000080; font-weight: bold;">while</span> (<span style="color: #000000;">have_posts</span>()) <span style="color: #000000;">:</span> <span style="color: #000000;">the_post</span>(); <span style="color: #008080;">?&gt;</span><br />
<span style="color: #000000;">//主循环内容</span><br />
<span style="color: #008080;">&lt;?php</span> <span style="color: #000080; font-weight: bold;">endwhile</span>; <span style="color: #000080; font-weight: bold;">endif</span>; <span style="color: #008080;">?&gt;</span></div>
<p>以上方法转自：芒果-<a id="title" title="WordPress 主循环排除特定分类" href="http://www.mangguo.org/wordpress-main-loop-exclude-specific-category/" target="_blank">WordPress 主循环排除特定分类</a></p>
<p><strong>二、Wordpress 主循环内排除特定分类：</strong></p>
<p>用途举例：现在可以看到我的每篇日志中左上角都有图片广告，如果某特定分类并不适合投放广告，或者说需要根据日志分类来显示不同的广告，这时候，你就需要用到 in_category 函数，在 the loop 之内排除 WordPress 的特定日志分类。</p>
<p>用法如下：</p>
<div class="source" style="font-family: 'Courier New','Lucida Console','monospace'; color: #000000;"><span style="color: #008080;">&lt;?php</span><br />
<span style="color: #000080; font-weight: bold;">if</span> ( <span style="color: #000000;">in_category</span>( <span style="color: #0000ff;">'pachoderms'</span> )) <span style="color: #000000;">{</span><br />
    <span style="font-style: italic; color: #008800;">// They have long trunks...</span><br />
<span style="color: #000000;">}</span> <span style="color: #000080; font-weight: bold;">elseif</span> ( <span style="color: #000000;">in_category</span>( <span style="color: #000080; font-weight: bold;">array</span>( <span style="color: #0000ff;">'Tropical Birds'</span><span style="color: #000000;">,</span> <span style="color: #0000ff;">'small-mammals'</span> ) )) <span style="color: #000000;">{</span><br />
    <span style="font-style: italic; color: #008800;">// They are warm-blooded...</span><br />
<span style="color: #000000;">}</span> <span style="color: #000080; font-weight: bold;">else</span> <span style="color: #000000;">{</span><br />
    <span style="font-style: italic; color: #008800;">// &amp; c.</span><br />
<span style="color: #000000;">}</span><br />
<span style="color: #008080;">?&gt;</span></div>
<p>in_category 函数的具体用法可以<a title="Function Reference/in category" href="http://codex.wordpress.org/Template_Tags/in_category" target="_blank">参见官方文件</a>。</p>
<p>WordPress 真是怎么学都学不完啊，感觉我越学越白痴，离 DIY 属于自己的主题又远了一步，囧rz……</p>
<div id="rl_posts">    <div id="rl_posts_hd" class="box_hd">        <span id="rl_posts_title" class="box_title">与 <h2 itemprop="keywords"><a href="http://wange.im/tag/wordpress/" title="WordPress" rel="tag">WordPress</a>,<a href="http://wange.im/tag/loop/" title="循环" rel="tag">循环</a></h2> 相关的文章        </span>    </div>    <div id="rl_posts_bd">        <ul class="clearfix"><li>    <div class="rl_thumb"><img src="http://wange.im/wp-content/themes/wange/imgs/category/computer.jpg" alt="电脑网络" width="45" height="45" itemprop="thumbnailUrl" />    </div>    <div class="rl_title">        <a href="http://wange.im/wordpress-isolated-latest-post.html" rel="bookmark" title="Wordpress 分离并调用第一篇最新日志">Wordpress 分离并调用第一篇最新日志</a>        <div class="rl_date">2011年05月30日</div>    </div></li><li>    <div class="rl_thumb"><img src="http://wange.im/wp-content/themes/wange/timthumb.php?src=http://wange.im/cache/wg-tags-tip.jpg&amp;h=45&amp;w=45&amp;zc=1" alt="万戈牌 WordPress 搜索自动匹配提示标签关键词" width="45" height="45" itemprop="thumbnailUrl" />    </div>    <div class="rl_title">        <a href="http://wange.im/wg-tags-tip.html" rel="bookmark" title="万戈牌 WordPress 搜索自动匹配提示标签关键词">万戈牌 WordPress 搜索自动匹配提示标签关键词</a>        <div class="rl_date">2012年04月7日</div>    </div></li><li>    <div class="rl_thumb"><img src="http://wange.im/wp-content/themes/wange/timthumb.php?src=http://wange.im/cache/screenshot.jpg&amp;h=45&amp;w=45&amp;zc=1" alt="新年新气象，博客换新衣" width="45" height="45" itemprop="thumbnailUrl" />    </div>    <div class="rl_title">        <a href="http://wange.im/new-year-new-theme.html" rel="bookmark" title="新年新气象，博客换新衣">新年新气象，博客换新衣</a>        <div class="rl_date">2012年01月20日</div>    </div></li><li>    <div class="rl_thumb"><img src="http://wange.im/wp-content/themes/wange/timthumb.php?src=http://wange.im/cache/2011-12-29_194903.jpg&amp;h=45&amp;w=45&amp;zc=1" alt="2011 年终总结" width="45" height="45" itemprop="thumbnailUrl" />    </div>    <div class="rl_title">        <a href="http://wange.im/2011-year-end-summary.html" rel="bookmark" title="2011 年终总结">2011 年终总结</a>        <div class="rl_date">2011年12月29日</div>    </div></li>        </ul>    </div></div>]]></content:encoded>
			<wfw:commentRss>http://wange.im/wordpress-exclude-category.html/feed</wfw:commentRss>
		<slash:comments>118</slash:comments>
		</item>
	</channel>
</rss>

