添加WordPress文章的时候提示过时失效方法

很多网站基本都会保持每天几篇文章的频率,但部分文章具有时效性,比如活动,比如某些工具等。这类文章不仅无法帮助找到这篇文章的人,反而会误导他们,白白浪费时间与精力。那么添加WordPress文章的时候提示过时失效方法。

输出WordPress数据库中存储的文章最后更新时间字段,并与访问者访问时间对比,如果访问时间大于最后更新时间+一天,则输出文章失效提示,反之则不输出。

利用WordPress提供的the_content()钩子挂载。当然,这样会导致所有页面中及摘要中都会出现提示。

在functions.php文件中加入如下代码

//文章过期提示

function article_time_update($content) {

date_default_timezone_set(‘PRC’);

$newdate=time();

$updated_date = get_the_modified_time(‘Y-m-d H:i:s’);

$updatetime=strtotime($updated_date);

$custom_content = ”;

if ( $newdate > $updatetime+86400) {

$custom_content= ‘<div class=”article-timeout”><strong>重要:</strong>本文最后更新于<code>’. $updated_date . ‘</code>,某些文章具有时效性,若有错误或已失效,请在下方<a href=”#comment”>留言</a>或联系<a target=”_blank” title=”微慕小程序” href=”https://www.minapper.com/about”><b>微慕小程序</b></a>。</div >’;

}

echo $custom_content.$content;

}

add_action(‘the_content’,’article_time_update’);

不建议使用此方法,因为部分主题可能会在摘要部分显示提示信息!

直接调用函数

如果你只需要文章提示,那么可以直接修改下你的文章模板single.php,在the_content()函数之前调用下如下函数。

先将下面的函数代码加入到你的functions.php文件中。

//文章过期提示

function article_time_update() {

date_default_timezone_set(‘PRC’);

$newdate=time();

$updated_date = get_the_modified_time(‘Y-m-d H:i:s’);

$updatetime=strtotime($updated_date);

$custom_content = ”;

if ( $newdate > $updatetime+86400) {

$custom_content= ‘<div class=”article-timeout”><strong>重要:</strong>本文最后更新于<code>’. $updated_date . ‘</code>,某些文章具有时效性,若有错误或已失效,请在下方<a href=”#comment”>留言</a>或联系<a target=”_blank” title=”微慕小程序” href=”https://www.minapper.com/about”><b>微慕小程序</b></a>。</div >’;

}

echo $custom_content;

}

在single.php文件中找到the_content()函数,并在其之前加上article_time_update()即可。

标签

发表评论