WordPress

推荐列表 站点导航

当前位置:首页 > 建站教程 > WordPress >

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

来源:网络整理  作者:  发布时间:2020-12-16 16:06
很多网站基本都会保持每天几篇文章的频率,但部分文章具有时效性,比如活动,比如某些工具等。这类文章不仅无...

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

添加WordPress文章的时候提示过时失效方法 (https://www.wpmee.com/) WordPress使用教程 第1张

输出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><strong>重要:</strong>本文最后更新于<code>'. $updated_date . '</code>,某些文章具有时效性,若有错误或已失效,请在下方<a href="#comment">留言</a>或联系<a target="_blank" title="陆壹主题" href="https://www.wpmee.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><strong>重要:</strong>本文最后更新于<code>'. $updated_date . '</code>,某些文章具有时效性,若有错误或已失效,请在下方<a href="#comment">留言</a>或联系<a target="_blank" title="陆壹主题" href="https://www.wpmee.com/about"><b>陆壹主题</b></a>。</div >';

}

echo $custom_content;

}

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

相关热词: 方法

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/jz/wp/4469.shtml

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

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

2020-12-16 编辑:

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

添加WordPress文章的时候提示过时失效方法 (https://www.wpmee.com/) WordPress使用教程 第1张

输出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><strong>重要:</strong>本文最后更新于<code>'. $updated_date . '</code>,某些文章具有时效性,若有错误或已失效,请在下方<a href="#comment">留言</a>或联系<a target="_blank" title="陆壹主题" href="https://www.wpmee.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><strong>重要:</strong>本文最后更新于<code>'. $updated_date . '</code>,某些文章具有时效性,若有错误或已失效,请在下方<a href="#comment">留言</a>或联系<a target="_blank" title="陆壹主题" href="https://www.wpmee.com/about"><b>陆壹主题</b></a>。</div >';

}

echo $custom_content;

}

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

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jz/wp/4469.shtml

相关文章

风云图片

推荐阅读

返回WordPress频道首页