平沙 發表於 2023-9-24 00:00:00

wordpress get_posts函数的使用方法 禁止输出指定类别的文章

<p>
<span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>使用wordpress禁止输出指定类别的文章可以给get_posts()函数传个数组参数,如下:</span></p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<p style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span></p>
<p>
代码如下:</p>
</div>
<p style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
&lt;div id="diary1"&gt;<br>
&lt;h3&gt;随机呈现&lt;/h3&gt;<br>
&lt;ul&gt;<br>
&lt;?php<br>
$args=array(<br>
'numberposts'=&gt;16,<br>
'category'=&gt;'-9,-12',<br>
'orderby'=&gt;'rand'<br>
);<br>
$rand_posts = get_posts($args);<br>
foreach( $rand_posts as $post ) :<br>
?&gt;<br>
&lt;li&gt;&lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/li&gt;<br>
&lt;?php endforeach; ?&gt;<br>
&lt;/ul&gt;<br>
&lt;/div&gt;</p>
<p>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>其中:</span></p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<p style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span></p>
<p>
代码如下:</p>
</div>
<p style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
$args=array(<br>
'numberposts'=&gt;16,<br>
'category'=&gt;'-9,-12',<br>
'orderby'=&gt;'rand'<br>
);</p>
<p>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>键名numberposts表示取出的文章数,category表示要显示的文章的类别ID,负数代表不显示,以字符串的形式用逗号隔开,orderby这里表示随机取出文章。</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>效果如小谈博客首页右侧“随机呈现”效果,去掉了php类别的文章显示,因为下面有了一个“php专栏”,避免重复。</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>get_posts()函数完整的参数列表:</span></p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<p style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span></p>
<p>
代码如下:</p>
</div>
<p style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
&lt;?php $args = array(<br>
'posts_per_page' =&gt; 5,<br>
'numberposts' =&gt; 5,<br>
'offset' =&gt; 0,<br>
'category' =&gt; '',<br>
'orderby' =&gt; 'post_date',<br>
'order' =&gt; 'DESC',<br>
'include' =&gt; '',<br>
'exclude' =&gt; '',<br>
'meta_key' =&gt; '',<br>
'meta_value' =&gt; '',<br>
'post_type' =&gt; 'post',<br>
'post_mime_type' =&gt; '',<br>
'post_parent' =&gt; '',<br>
'post_status' =&gt; 'publish',<br>
'suppress_filters' =&gt; true );<br>
?&gt;</p>
頁: [1]
查看完整版本: wordpress get_posts函数的使用方法 禁止输出指定类别的文章