[WordPress] How to automatically display “old information” on posts that have passed a certain period of time since the last update date
- Published:
category: WordPress
If you keep writing articles in WordPress, I think that there are posts that are left as they are without updating, although it can not be helped. However, I think that some people dare not update the diary because if it is updated, it will not be the event at that time.
Some people may think, “No, it’s okay because the release date and update date are displayed under the title of the page!”, But when you actually see various articles, that part Are you sure? You haven’t done that, I’m sure.
As much as that, especially information on the Internet is often overlooked.
Well, if the content of all the articles is universal in the future, there will be no problem, but if you make a mistake, you will read old information that can no longer be used, and the site itself says, “This site information is incorrect.” There is a possibility that the recognition of the information will be mistaken, and some people may post it on SNS etc. in a biased manner.
Therefore, this time, in order to avoid such troubles, when accessing a post created and managed by WordPress for a certain period of time from the last update date, “may be old information” is automatically displayed. I will show you how.
This doesn’t solve all the problems, but it may be necessary to create a more friendly site.
Display example to inform that it is an old article
If you implement it using the method of this article, you will be able to display the following on the text of posts that have passed a certain period of time.
Since the Japanese page is translated into English, the above figure is displayed in Japanese.
The date is automatically extracted from the data and displayed.
Skills required for customizing with reference to this article
The following conditions and skills are required to implement the contents of this article on your own site.
- WordPress site operated using child themes
- Those who can upload / download files on the server using FTP etc.
- Those who can understand the description and output flow in the post template (single.php) to some extent
- Addition of theme Those who can add design code to CSS
In addition, this article is an implementation example of the “ha-Basic” theme that is also used on the site you are viewing, so you need to adjust it according to the theme used on your own site.
How to customize to automatically display that information may be out of date
1. Copy the parent theme post template to the child theme
Use FTP or your server’s file manager to copy the parent theme’s post template (usually single.php) into the child theme.
2. Add code to child theme post template
Check the content of the post template copied to the child theme, and the post body is output
the_content();
Add the following code above the code called, or above the hook etc. where this code is output.
Please check carefully as the way to write the post template differs depending on the theme.
For ha-Basic theme, in single.php
<!--本文取得-->
It is OK if you add it directly above.
Code to add to gle.php
<!-- Display to old articles -->
<?php
$keikanissuu = 365;
if ( date( 'U' ) - get_the_modified_time( 'U' ) > 60 * 60 * 24 * $keikanissuu ) : ?>
<div class="ha-old-moddified"><p class="is-ha-para-notice">Last updated on this page:<?php echo get_the_modified_date(); ?>.Please note that the information may be out of date.</p>
</div>
<?php endif; ?>
After saving the article, display the old article that has not been updated for more than a year and check if it is output in the desired location (conversely, if the article is not old, it will not be displayed).
Code description
The variable $ keikanissuu at the beginning specifies the number of days since the last update date (365 days = 1 year’s worth of days in the example).
In the if … line, it is judged whether to display or not to display according to the number of days since the last update date.
The <div … line indicates the content to be displayed.
For post templates composed of PHP, you need to put PHP closing and starting tags before and after the code, or reformat it as a series of PHP syntax (otherwise you can display the page with an error). Please note that it may disappear)
3. Add theme Add design code to CSS
If you are using the “ha-Basic” theme, this is not necessary
Add the following code to the theme addition CSS
/* Note */
.is-style-ha-para-notice:before {
font-family: 'icomoon';
content: "\ea07";
color: #ffc533;
padding-right: 16px;
font-weight: bold;
font-size: 1.2em;
}
.is-style-ha-para-notice {
background: #fff9e6;
padding: 8px;
border: 2px solid #ffc533;
border-radius: 8px;
}
The caution mark at the beginning is an example using the icon font called icomoon, so it may not be displayed depending on the theme you are using, so please change and adjust as appropriate.
Now, the guide to the old article will be output according to the number of days since the last update date.
Reference page:WordPress:古い記事にメッセージを表示させる際の備忘録
There is also a way to deal with it by adding it to the theme’s functions.php …
So far, I’ve shown you how to add and implement code directly into the theme’s single.php (post template).
After reading this far, you may be wondering, “Why don’t you use the standard WordPress hook to automatically add it to the body of your post?”
Of course, it can be implemented by that method, and even if some changes are made to single.php by updating the theme, it seems convenient at first glance, but 〇 characters from the beginning of the post body The following inconveniences may occur in the case of a theme with specifications that output to the description tag in the site or the description tag in HTML.
- At the beginning of the description of the post list on the site, a guide text is displayed to inform that it is an old article
- At the beginning of the description of the search result of the search engine, a guide text is displayed to inform that it is an old article.
This is because it is instructed “a part of the text, at the beginning” by using the hook mentioned above, so I think that it is a bit sick in terms of operation, so in this article I will introduce how to write it directly in single.php doing.
Therefore, in order to avoid confusion in this article, I will refrain from introducing specific code, so for how to insert something using a hook at the beginning of the post body, search for “WordPress body beginning fixed phrase” etc. If you do, you will get various hints, so please check and implement it yourself (be sure to check how it is output after implementation).
Techniques you want to use together (how to control the update date)
In the case of updating a post, you may also make minor updates such as:
- Update to fix only typographical errors and omissions
- Replacing featured images and images in the text
- Correct the association of categories and tags
In the method introduced this time, when you click “Update” on the post edit screen of WordPress, the information is automatically changed by referring to the information of the last update date, so it is judged whether it is an old post or not. It may be inconvenient if the update date is changed with a minor update.
Therefore, by making it possible to perform “setting whether to update the update date” on the post edit screen by the method of the linked article below, the update date will not be changed for the above minor edits. It is possible to do so, so we recommend that you use it together.
※ha-Basicテーマでは標準で装備されています
Post Author: Knowledge Base Admin