WordPress文章标题为空时其它内容代替
2年前 (2020-11-15)
在WordPress中时常存在某些文章不需要标题的情况,特别是在一些个人网站,他们常常使用一些不需要标题的post_format来写自己的即时心情、日志、状态等,但我们都知道,如果没有标题,会造成很多不好的结果:没有标题显示为空,没有办法点击进入详细页面,网页的标题为空,对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两个函数时进行一个判断,如果发现这个文章没有填写标题,就会用post_format @ post_time的形式作为标题。注意:the_title和get_the_title必须在LOOP中使用。