查看: 66|回复: 0

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

[复制链接]

5

主题

0

回帖

0

积分

热心网友

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2010-1-13
发表于 2023-9-24 00:00:00 | 显示全部楼层 |阅读模式

使用wordpress禁止输出指定类别的文章可以给get_posts()函数传个数组参数,如下:

复制代码

代码如下:


<div id="diary1">
<h3>随机呈现</h3>
<ul>
<?php
$args=array(
'numberposts'=>16,
'category'=>'-9,-12',
'orderby'=>'rand'
);
$rand_posts = get_posts($args);
foreach( $rand_posts as $post ) :
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
</div>


其中:

复制代码

代码如下:


$args=array(
'numberposts'=>16,
'category'=>'-9,-12',
'orderby'=>'rand'
);


键名numberposts表示取出的文章数,category表示要显示的文章的类别ID,负数代表不显示,以字符串的形式用逗号隔开,orderby这里表示随机取出文章。
效果如小谈博客首页右侧“随机呈现”效果,去掉了php类别的文章显示,因为下面有了一个“php专栏”,避免重复。
get_posts()函数完整的参数列表:

复制代码

代码如下:


<?php $args = array(
'posts_per_page' => 5,
'numberposts' => 5,
'offset' => 0,
'category' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true );
?>

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部