WordPress禁止显示指定类别的文章

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

<div class="widget" 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 );
?>

  • 10
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值