WordPress教程:超过一定时间的老文章顶部显示自定义信息

前端之家收集整理的这篇文章主要介绍了WordPress教程:超过一定时间的老文章顶部显示自定义信息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

站长朋友们在wordpress插件库查找插件时,偶尔会看到提示插件已经超过 2 年没有更新。如果我们想在自己的wordpress文章顶部显示类似的提示信息,比如超过 1 年没有更新的文章,就提示某些信息,如下图所示:

这篇wordpress教程里,我们就向大家介绍一下实现的方法。切换到主题目录,打开 functions.PHP 文件,加入以下代码

Crayon-5c891cbd7fca2229907978" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">functions.PHP
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="Copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-language">PHP
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891cbd7fca2229907978-1">1
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cbd7fca2229907978-2">2
Crayon-num" data-line="Crayon-5c891cbd7fca2229907978-3">3
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cbd7fca2229907978-4">4
Crayon-num" data-line="Crayon-5c891cbd7fca2229907978-5">5
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cbd7fca2229907978-6">6
Crayon-num" data-line="Crayon-5c891cbd7fca2229907978-7">7
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cbd7fca2229907978-8">8
Crayon-num" data-line="Crayon-5c891cbd7fca2229907978-9">9
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cbd7fca2229907978-10">10
Crayon-num" data-line="Crayon-5c891cbd7fca2229907978-11">11
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891cbd7fca2229907978-1">Crayon-c">//超过一定时间的老文章顶部显示自定义信息
Crayon-line Crayon-striped-line" id="Crayon-5c891cbd7fca2229907978-2">Crayon-t">functionCrayon-h"> Crayon-e">old_content_messageCrayon-sy">(Crayon-v">$contentCrayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line" id="Crayon-5c891cbd7fca2229907978-3">Crayon-h"> Crayon-v">$modifiedCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">get_the_modified_timeCrayon-sy">(Crayon-s">'U'Crayon-sy">)Crayon-sy">;Crayon-h">
Crayon-line Crayon-striped-line" id="Crayon-5c891cbd7fca2229907978-4">Crayon-h"> Crayon-v">$currentCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">current_timeCrayon-sy">(Crayon-s">'timestamp'Crayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891cbd7fca2229907978-5">Crayon-h"> Crayon-v">$diffTimeCrayon-h"> Crayon-o">=Crayon-h"> Crayon-sy">(Crayon-v">$currentCrayon-h"> Crayon-o">-Crayon-h"> Crayon-v">$modifiedCrayon-sy">)Crayon-h"> Crayon-o">/Crayon-h"> Crayon-sy">(Crayon-cn">60Crayon-o">*Crayon-cn">60Crayon-o">*Crayon-cn">24Crayon-sy">)Crayon-sy">;Crayon-h">
Crayon-line Crayon-striped-line" id="Crayon-5c891cbd7fca2229907978-6">Crayon-h"> Crayon-st">ifCrayon-sy">(Crayon-v">$diffTimeCrayon-h"> Crayon-h"> Crayon-cn">365Crayon-h"> Crayon-sy">)Crayon-sy">{
Crayon-line" id="Crayon-5c891cbd7fca2229907978-7">Crayon-h"> Crayon-v">$contentCrayon-h"> Crayon-o">=Crayon-h"> Crayon-s">'
Crayon-sy">.Crayon-e">get_the_modified_timeCrayon-sy">(Crayon-s">'Y年n月j日'Crayon-sy">)Crayon-sy">.Crayon-s">',已超过 1 年没有更新,如果文章内容失效,请反馈给我们,谢谢!
'
Crayon-sy">.Crayon-v">$contentCrayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891cbd7fca2229907978-8">Crayon-h"> Crayon-sy">}
Crayon-line" id="Crayon-5c891cbd7fca2229907978-9">Crayon-h"> Crayon-k ">returnCrayon-h"> Crayon-v">$contentCrayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891cbd7fca2229907978-10">Crayon-sy">}
Crayon-line" id="Crayon-5c891cbd7fca2229907978-11">Crayon-e">add_filterCrayon-sy">(Crayon-h"> Crayon-s">'the_content'Crayon-sy">,Crayon-h"> Crayon-s">'old_content_message'Crayon-h"> Crayon-sy">)Crayon-sy">;

以上代码的第 3 行使用了 get_the_modified_time() 函数获取该篇文章的最后修订时间,如果您的文章在发布后进行过修改,比如修改标签内容等都会更新时间。第 6 行的 365 是天数,第 7 行 是自定义信息,请根据您自己的需要进行修改

如果您只想在特定的分类显示提示,比如只在 ID 为 10 的分类文章显示提示信息,可以将第 6 行代码修改为:

Crayon-5c891cbd7fcac521260527" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">functions.PHP
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="Copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-language">PHP
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891cbd7fcac521260527-1">1
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891cbd7fcac521260527-1">Crayon-st">ifCrayon-sy">(Crayon-v">$diffTimeCrayon-h"> Crayon-h"> Crayon-cn">365Crayon-h"> Crayon-o">&&Crayon-h"> Crayon-e">in_categoryCrayon-sy">(Crayon-cn">10Crayon-sy">)Crayon-h"> Crayon-sy">)Crayon-sy">{

如果是多个分类,比如在 ID 为 10、11、12的分类文章显示提示信息,可以使用数组形式:

Crayon-5c891cbd7fcae052465059" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">functions.PHP
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="Copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-language">PHP
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891cbd7fcae052465059-1">1
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891cbd7fcae052465059-1">Crayon-st">ifCrayon-sy">(Crayon-v">$diffTimeCrayon-h"> Crayon-h"> Crayon-cn">365Crayon-h"> Crayon-o">&&Crayon-h"> Crayon-e">in_categoryCrayon-sy">(Crayon-t">arrayCrayon-sy">(Crayon-cn">10Crayon-sy">,Crayon-cn">11Crayon-sy">,Crayon-cn">12Crayon-sy">)Crayon-sy">)Crayon-h"> Crayon-sy">)Crayon-sy">{

如果要排除某些分类,比如在 ID 为 10、11、12 以外的分类文章显示提示信息,可在 in_category 前添加“!”:

Crayon-5c891cbd7fcb0477788446" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">functions.PHP
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="Copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-language">PHP
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891cbd7fcb0477788446-1">1
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891cbd7fcb0477788446-1">Crayon-st">ifCrayon-sy">(Crayon-v">$diffTimeCrayon-h"> Crayon-h"> Crayon-cn">365Crayon-h"> Crayon-o">&&Crayon-h"> Crayon-o">!Crayon-e">in_categoryCrayon-sy">(Crayon-t">arrayCrayon-sy">(Crayon-cn">10Crayon-sy">,Crayon-cn">12Crayon-sy">)Crayon-sy">)Crayon-h"> Crayon-sy">)Crayon-sy">{

然后在css文件添加如下样式,可以达到配图效果

Crayon-5c891cbd7fcb2418941276" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">styles.css
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="Copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-language">CSS
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891cbd7fcb2418941276-1">1
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891cbd7fcb2418941276-1">Crayon-k ">.old-messageCrayon-sy">{Crayon-e ">paddingCrayon-sy">:Crayon-i ">10pxCrayon-sy">;Crayon-e ">colorCrayon-sy">:Crayon-h"> Crayon-i ">#DB7C22Crayon-sy">;Crayon-e ">font-sizeCrayon-sy">:Crayon-h"> Crayon-i ">14pxCrayon-sy">;Crayon-e ">backgroundCrayon-sy">:Crayon-h"> Crayon-i ">#FFFCEFCrayon-sy">;Crayon-e ">borderCrayon-sy">:Crayon-h"> Crayon-i ">solidCrayon-h"> Crayon-i ">1pxCrayon-h"> Crayon-i ">#FFBB76Crayon-sy">;Crayon-e ">border-radiusCrayon-sy">:Crayon-h"> Crayon-i ">2pxCrayon-sy">;Crayon-e ">Box-shadowCrayon-sy">:Crayon-h"> Crayon-i ">0Crayon-h"> Crayon-i ">0Crayon-h"> Crayon-i ">3pxCrayon-h"> Crayon-i ">#dddCrayon-sy">;Crayon-sy">}

需要此功能的朋友可以按照上面的方法进行操作。

原文链接:https://www.f2er.com/wordpress/238367.html

猜你在找的wordpress相关文章