心婼向洋 發表於 2023-6-4 00:00:00

WordPress中重置文章循环的rewind_posts()函数讲解

<p>
有些时候,在索引页中(首页、分类文章、标签文章、作者文章索引……)提前进入 WordPress 文章循环中( Loop ),以获得一些我们想要获得的信息,但 WP 中,单一页面一般只会一次性跳入循环,也就是说,我们下次再从循环中汲取信息的时候,我们将获得循环中第二篇日志的信息,为了解决这一尴尬局面,WordPress 内置了一个函数,rewind_posts()函数专门用来重置循环指针。</p>
<p>
<strong>Description 描述</strong><br>
Rewind the loop posts.<br>
重置文章循环。</p>
<p>
<strong>使用</strong><br>
该函数不接受变量。</p>
<div>
<div>
<div id="highlighter_169789">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>&lt;?php rewind_posts(); ?&gt;</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
<strong>实例</strong><br>
在此引用 WordPress 默认主题 twenty eleven 中,author.php 文件 第15-55行,并予以简化。</p>
<div>
<div>
<div id="highlighter_358918">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
<div>
4</div>
<div>
5</div>
<div>
6</div>
</td>
<td>
<div>
<div>
<code>&lt;?php </code>
</div>
<div>
<code>if</code> <code>( have_posts() ) : the_post();</code><code>//进入循环</code>
</div>
<div>
<code>  </code><code>echo</code> <code>get_the_author() ; </code><code>//显示文章作者,在循环外使用需指定作者ID</code>
</div>
<div>
<code> </code><code>rewind_posts();</code><code>//重置循环</code>
</div>
<div>
<code> </code><code>while</code> <code>( have_posts() ) : the_post(); </code><code>//循环开始</code>
</div>
<div>
<code>   </code><code>get_template_part( </code><code>'content'</code><code>, get_post_format() );</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
<strong>总结</strong><br>
有的时候,WordPress 有的函数必须进入循环才能使用,所以我们为了达到某些目的不得不提前进入循环,但如果我们在接下来的代码中在此用到循环,那循环会跳到第二篇的位置,所以我们不得不用到 rewind_posts() 来重置循环。twenty eleven 主题中只是给了一个很简单的例子,具体什么时候要用到还是要看主题作者的使用环境而言,也许有的时候我们要在一个页面中循环两次甚至更多次文章,那么改函数用处会更大。</p>
<p>
<strong>PS:single_cat_title()函数</strong><br>
single_cat_title()函数,日常中我们很少会用到,但这个函数会给我们解决很多问题,诸如当前页面的目录、标签,该函数不依附于 WordPress 主循环中,也不能放入主循环中使用。</p>
<p>
描述<br>
获取当前页面的分类、标签。</p>
<div>
<div>
<div id="highlighter_77909">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>&lt;?php single_cat_title(</code><code>$prefix</code><code>,</code><code>$display</code><code>); ?&gt;</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<ul>
<li>
$prefix :用于设置在标题之前显示的内容。</li>
<li>
$display :用于设置是直接显示还是返回到变量。</li>
</ul>
<p>
实例<br>
在此摘取 WordPress 2011 默认主题中,category.php 文件 第18行左右位置的代码</p>
<div>
<div>
<div id="highlighter_994507">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
</td>
<td>
<div>
<div>
<code>&lt;?php</code>
</div>
<div>
<code>printf( __( </code><code>'Category Archives: %s'</code><code>, </code><code>'twentyeleven'</code> <code>), </code><code>'&lt;span&gt;'</code> <code>. single_cat_title( </code><code>''</code><code>, false ) . </code><code>'&lt;/span&gt;'</code> <code>);</code>
</div>
<div>
<code>?&gt;</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
頁: [1]
查看完整版本: WordPress中重置文章循环的rewind_posts()函数讲解