在这个信息爆炸的时代,我们缺的不是大量的信息,而是准确有效的信息。可能我们前两年写得文章内容已经跟现在的情况不太符合了,特别是一些教程攻略,可能随着软件、数据的更新不再准确;又或者,文章中外链的资源地址可能失效等意外情况的发生。因此,从访客体验的角度考虑,我们可以对长期未更新的旧文章做一个提示,告诉读者这篇文章已经长期未更新,请注意文章准确性;或者如果文章内容或图片资源失效,请留言反馈,我们会及时处理等。
代码部署:
首先:打开【/inc/options/begin-options.php】文件,在适当位置添加代码
$options[] = array(
'name' => '文章提醒信息',
'desc' => '显示',
'id' => 'remind_content',
'class' => 'be_ico',
'std' => '0',
'type' => 'checkbox'
);
$options[] = array(
'name' => '',
'desc' => '超过几天提醒',
'id' => 'remind_over_time',
'class' => 'be_ico hidden',
'std' => '365',
'type' => 'text'
);
$options[] = array(
'name' => '',
'desc' => '提醒内容',
'id' => 'remind_message',
'class' => 'be_ico hidden',
'std' => '如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!',
'type' => 'text'
);
$options[] = array(
'name' => '',
'desc' => '提醒位置:文章顶部,默认提醒位置为文章底部',
'id' => 'remind_position',
'class' => 'be_ico hidden',
'std' => '',
'type' => 'checkbox'
);
$options[] = array(
'name' => '',
'desc' => '输入提醒的分类 ID,多个分类用英文半角逗号","隔开',
'id' => 'remind_id',
'class' => 'hidden',
'std' => '',
'type' => 'text'
);
$options[] = array(
'name' => '分类 ID 对照',
'desc' => '<ul>'.$cats_id.'</ul>',
'id' => 'catid',
'class' => 'remind-catid hidden',
'type' => 'info'
);
$options[] = array(
'id' => 'clear'
);
添加代码后,就可以在“后台-外观-主题选项-首页设置”看到这个功能了。
然后:打开【/inc/options/includes/themes-options.php】文件,在适当位置添加代码
jQuery('#remind_content').click(function() {
jQuery('#section-remind_over_time, #section-remind_message, #section-remind_position, #section-remind_id, .remind-catid').fadeToggle(400);
});
if (jQuery('#remind_content:checked').val() !== undefined) {
jQuery('#section-remind_over_time, #section-remind_message, #section-remind_position, #section-remind_id, .remind-catid').show();
}
这段代码主要就是给上面的功能添加展开/闭合功能,当不启用该功能时,该功能下的选项将被隐藏。
接着:打开【/inc/inc.php】文件,在适当位置添加代码
// 文章提醒信息
if (zm_get_option('remind_content')) {
function remind_content($content) {
$modified_time = get_the_modified_time('U');
$current_time = current_time('timestamp');
$diff_time = ($current_time - $modified_time) / (60 * 60 * 24);
$remind_over_time = zm_get_option('remind_over_time');
if ($diff_time > $remind_over_time && in_category(explode(',',zm_get_option('remind_id'))) ) {
if (zm_get_option('remind_position')) {
$content = '<div class="remind_content">'.zm_get_option('remind_message').'</div>'. $content;
} else {
$content = $content.'<div class="remind_content">'.zm_get_option('remind_message').'</div>';
}
}
return $content;
}
add_filter('the_content', 'remind_content');
}
这段代码是该功能的主体代码。
最后:打开【style.css】文件,在适当位置添加代码
/* 文章提醒信息 */.remind_content {
color: #db7c22;
background: #fffcef;
border: 1px solid #ffbb76;
border-radius: 2px;
overflow: hidden;
margin: 20px 0;
padding: 10px 15px;
font-size: 14px;
}
完成上面的操作后,在【主题选项】中找到该功能并启用,然后根据自己的情况设置相关的参数即可。
四、其他说明
1、参数说明
- 超过几天提醒:比如超过 1 年的文章显示提醒信息就输入365,要使文章一发布就显示提醒信息就输入0
- 提醒内容:可输入任意文字,不仅限于提醒类文字,但只能是纯文本文字
- 提醒位置:默认是文章底部,勾选时为文章顶部(文章标题下面)
- 提醒分类 ID:输入想要显示提醒信息的分类 ID 即可,多个分类用英文逗号","隔开
2、其他说明
使用相同主题的童鞋,直接按本文提供的操作步骤添加代码即可。使用其他主题的童鞋,可以参考本文的思路自行修改相关代码,也可以参考下面的代码
function remind_content($content) {
$modified_time = get_the_modified_time('U');
$current_time = current_time('timestamp');
$diff_time = ($current_time - $modified_time) / (60 * 60 * 24);
if ($diff_time > 365 ) {
// 提醒位置:文章顶部
// $content = '<div class="remind_content">提醒内容</div>'. $content;
// 提醒位置:文章底部
$content = $content.'<div class="remind_content">提醒内容</div>';
}
return $content;
}
add_filter('the_content', 'remind_content');
代码中需要修改的内容:
365
:超过的天数提醒内容
:修改为你想要设定的文字提醒位置
:默认显示在文章底部,若要让提醒信息显示在文章顶部,就恢复上面那条注释并注释下面那条代码
将修改后的代码保存到主题目录下的【functions.php】文件中,然后再复制上面的 CSS 代码到主题目录下的【style.css】文件中即可。