使用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 );
?>