在WordPress中時常存在某些文章不需要標題的情況,特別是在一些個人網(wǎng)站,他們常常使用一些不需要標題的post_format來寫自己的即時心情、日志、狀態(tài)等,但我們都知道,如果沒有標題,會造成很多不好的結(jié)果:沒有標題顯示為空,沒有辦法點擊進入詳細頁面,網(wǎng)頁的標題為空,對seo不好,不夠美觀
當然,如果只是一個個人的口袋站,完全不用理會前面兩點,但對于一些比較懶的站長,其實也需要注意這些問題。這里提供一個簡單的方法來處理這種情況:
function filter_post_empty_title($title){
$format = get_post_format();
if($title == $post_id || $title == ''){
$time = get_the_time('Y-m-d H:i:s');
$title = get_post_format_string($format).' @ '.$time;
}
return $title;
}
add_filter('the_title','filter_post_empty_title');
add_filter('get_the_title','filter_post_empty_title');
將上面這段代碼放在functions.php中,它可以幫助你在你的主題中使用the_title、get_the_title兩個函數(shù)時進行一個判斷,如果發(fā)現(xiàn)這個文章沒有填寫標題,就會用post_format @ post_time的形式作為標題。注意:the_title和get_the_title必須在LOOP中使用。