wordpress文章标题为空时其它内容代替的方法
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>在WordPress中时常存在某些文章不需要标题的情况,特别是在一些个人网站,他们常常使用一些不需要标题的post_format来写自己的即时心情、日志、状态等,但我们都知道,如果没有标题,会造成很多不好的结果:没有标题显示为空,没有办法点击进入详细页面,网页的标题为空,对seo不好,不够美观<br>
当然,如果只是一个个人的口袋站,完全不用理会前面两点,但对于一些比较懒的站长,其实也需要注意这些问题。这里提供一个简单的方法来处理这种情况:</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode5" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
function filter_post_empty_title($title){<br>
$format = get_post_format();<br>
if($title == $post_id || $title == ''){<br>
$time = get_the_time('Y-m-d H:i:s');<br>
$title = get_post_format_string($format).' @ '.$time;<br>
}<br>
return $title;<br>
}<br>
add_filter('the_title','filter_post_empty_title');<br>
add_filter('get_the_title','filter_post_empty_title');</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
将上面这段代码放在functions.php中,它可以帮助你在你的主题中使用the_title、get_the_title两个函数时进行一个判断,如果发现这个文章没有填写标题,就会用post_format @ post_time的形式作为标题。注意:the_title和get_the_title必须在LOOP中使用。</p>
頁:
[1]