隨着 WordPress 這款 php+mysql 的建站程序熱門起來之後很多資源分享站也開始使用 WordPress 來搭建了,由於是資源分享站,所以有時候一篇文章就要上傳多個文件,很多時候網站管理員需要了解某篇文章中有沒有附件,還需要點擊到文章的編輯頁面才能知道該篇文章有沒有附件這樣管理起來就非常不方便,今天小編就教大家如何在 WordPress 後台的文章列表頁直接顯示文章包含了多少個附件,代碼如下:
add_filter('manage_posts_columns', 'wxd_attachment_count', 5);
add_action('manage_posts_custom_column', 'wxd_columns_attachment_count', 5, 2);
function wxd_attachment_count($defaults){
$defaults['wps_post_attachments'] = __('附件數量');
return $defaults;
}
function wxd_columns_attachment_count($column_name, $id){
if($column_name === 'wps_post_attachments'){
$attachments = get_children(array('post_parent'=>$id));
$count = count($attachments);
if($count !=0){echo $count;}
}
}
將以上代碼加入到當前主題的 functions.php 文件即可下面是效果圖:

如果文章中包含附件則會顯示附件的數量,如果不包含則什麼都不顯示。