WordPress中用于获取文章作者与分类信息的方法整理
<p><strong>作者</strong><br>
查询和某些作者(用户)有关的文章,可以使用 4 个参数:</p>
<ol>
<li>
author(整数):用户 ID</li>
<li>
author_name(字符串):用户的昵称(“user_nicename” 字段)</li>
<li>
author__in(数组):用户 ID</li>
<li>
author__not_in(数组):用户 ID</li>
</ol>
<p>
获取一个作者的文章</p>
<p>
根据用户 ID 获取:</p>
<div>
<div>
<div id="highlighter_730136">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>$query</code> <code>= </code><code>new</code> <code>WP_Query( </code><code>'author=123'</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
根据用户的昵称(“user_nicename” 字段)获取:</p>
<div>
<div>
<div id="highlighter_284667">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>$query</code> <code>= </code><code>new</code> <code>WP_Query( </code><code>'author_name=rami'</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
获取多个作者的文章</p>
<p>
根据用户 ID 获取多个作者的文章:</p>
<div>
<div>
<div id="highlighter_805502">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>$query</code> <code>= </code><code>new</code> <code>WP_Query( </code><code>'author=2,6,17,38'</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
排除作者的文章</p>
<p>
排除一个作者可以使用用户 ID 前边加上 “-” 的方式:</p>
<div>
<div>
<div id="highlighter_520364">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>$query</code> <code>= </code><code>new</code> <code>WP_Query( </code><code>'author=-12'</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
多作者查询</p>
<p>
获取多个作者的文章:</p>
<div>
<div>
<div id="highlighter_658982">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>$query</code> <code>= </code><code>new</code> <code>WP_Query( </code><code>array</code><code>( </code><code>'author__in'</code> <code>=> </code><code>array</code><code>( 2, 6 ) ) );</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
排除多个作者的文章:</p>
<div>
<div>
<div id="highlighter_787248">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>$query</code> <code>= </code><code>new</code> <code>WP_Query( </code><code>array</code><code>( </code><code>'author__not_in'</code> <code>=> </code><code>array</code><code>( 2, 6 ) ) );</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
<strong>分类</strong></p>
<p>
关于分类有 5 个可用的参数:</p>
<ol>
<li>
cat(整数):分类 ID</li>
<li>
category_name(字符串):分类别名</li>
<li>
category__and(数组):分类 ID</li>
<li>
category__in(数组):分类 ID</li>
<li>
category__not_in(数组):分类 ID</li>
</ol>
<p>
获取一个分类的文章</p>
<p>
根据分类的 ID 获取一个分类的文章(包含此分类的子分类):</p>
<div>
<div>
<div id="highlighter_809503">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>$query</code> <code>= </code><code>new</code> <code>WP_Query( </code><code>'cat=4'</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
根据分类的别名获取一个分类的文章(包含此分类的子分类):</p>
<div>
<div>
<div id="highlighter_725864">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>$query</code> <code>= </code><code>new</code> <code>WP_Query( </code><code>'category_name=staff'</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
根据分类的 ID 获取一个分类的文章(不包含此分类的子分类):</p>
<div>
<div>
<div id="highlighter_948317">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>$query</code> <code>= </code><code>new</code> <code>WP_Query( </code><code>'category__in=4'</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
获取多个分类的文章</p>
<p>
根据分类的 ID 获取多个分类的文章:</p>
<div>
<div>
<div id="highlighter_787366">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>$query</code> <code>= </code><code>new</code> <code>WP_Query( </code><code>'cat=2,6,17,38'</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
根据分类别名获取多个分类的文章:</p>
<div>
<div>
<div id="highlighter_519051">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>$query</code> <code>= </code><code>new</code> <code>WP_Query( </code><code>'category_name=staff,news'</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
根据分类别名获取同时拥有几个分类的文章:</p>
<div>
<div>
<div id="highlighter_894696">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>$query</code> <code>= </code><code>new</code> <code>WP_Query( </code><code>'category_name=staff+news'</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
排除分类</p>
<p>
想要排除一些分类只需要在分类 ID 前加上 “-” 号:</p>
<div>
<div>
<div id="highlighter_240784">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>$query</code> <code>= </code><code>new</code> <code>WP_Query( </code><code>'cat=-12,-34,-56'</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
多分类查询</p>
<p>
获取同时有多个分类的文章,下边的代码会获取必须同时拥有 ID 为 2 和 6 的分类的文章:</p>
<div>
<div>
<div id="highlighter_255095">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>$query</code> <code>= </code><code>new</code> <code>WP_Query( </code><code>array</code><code>( </code><code>'category__and'</code> <code>=> </code><code>array</code><code>( 2, 6 ) ) );</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
获取多个分类的文章,不包含其子分类,下边的代码获取了拥有 ID 为 2 或者 ID 为 6 的分类的文章:</p>
<div>
<div>
<div id="highlighter_496948">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>$query</code> <code>= </code><code>new</code> <code>WP_Query( </code><code>array</code><code>( </code><code>'category__in'</code> <code>=> </code><code>array</code><code>( 2, 6 ) ) );</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
排除一些分类的文章:</p>
<div>
<div>
<div id="highlighter_220866">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>$query</code> <code>= </code><code>new</code> <code>WP_Query( </code><code>array</code><code>( </code><code>'category__not_in'</code> <code>=> </code><code>array</code><code>( 2, 6 ) ) );</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
頁:
[1]