在WordPress中時(shí)常存在某些文章不需要標(biāo)題的情況,特別是在一些個(gè)人網(wǎng)站,他們常常使用一些不需要標(biāo)題的post_format來寫自己的即時(shí)心情、日志、狀態(tài)等,但我們都知道,如果沒有標(biāo)題,會(huì)造成很多不好的結(jié)果:沒有標(biāo)題顯示為空,沒有辦法點(diǎn)擊進(jìn)入詳細(xì)頁面,網(wǎng)頁的標(biāo)題為空,對seo不好,不夠美觀
當(dāng)然,如果只是一個(gè)個(gè)人的口袋站,完全不用理會(huì)前面兩點(diǎn),但對于一些比較懶的站長,其實(shí)也需要注意這些問題。這里提供一個(gè)簡單的方法來處理這種情況:
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兩個(gè)函數(shù)時(shí)進(jìn)行一個(gè)判斷,如果發(fā)現(xiàn)這個(gè)文章沒有填寫標(biāo)題,就會(huì)用post_format @ post_time的形式作為標(biāo)題。注意:the_title和get_the_title必須在LOOP中使用。