I run a news website, and I think there are considerable benefits to adding a last date updated option to it. However, everything I see online is for replacing the published date with the date modified. I want both, like in this article by the NYT.
What's more, I don't want this on every article since I edit articles a lot and don't always want each of them to reflect it. Instead, I want it for "evergreen" content that has proven popular long after it was published; my hope is that Google shows the more recent time in search results. As a result, I prefer doing this with a custom field or a CF plugin -- I currently have CMB2 installed.
The not-very-helpful theme I use uses the below function to add dates to posts:
if (!function_exists('mh_magazine_post_meta')) {
function mh_magazine_post_meta() {
$mh_magazine_options = mh_magazine_theme_options();
if ($mh_magazine_options['post_meta_date'] === 'enable' || $mh_magazine_options['post_meta_author'] === 'enable' && in_the_loop() || $mh_magazine_options['post_meta_cat'] === 'enable' && in_the_loop() && is_singular() || $mh_magazine_options['post_meta_comments'] === 'enable') {
echo '<div class="mh-meta entry-meta">' . "\n";
if ($mh_magazine_options['post_meta_date'] === 'enable') {
echo '<span class="entry-meta-date updated"><i class="fa fa-clock-o"></i><a href="' . esc_url(get_month_link(get_the_time('Y'), get_the_time('m'))) . '">' . get_the_date() . '</a></span>' . "\n";
}
if ($mh_magazine_options['post_meta_author'] === 'enable' && in_the_loop()) {
echo '<span class="entry-meta-author author vcard"><i class="fa fa-user"></i><a class="fn" href="' . esc_url(get_author_posts_url(get_the_author_meta('ID'))) . '">' . esc_html(get_the_author()) . '</a></span>' . "\n";
}
if ($mh_magazine_options['post_meta_cat'] === 'enable' && in_the_loop() && is_singular()) {
echo '<span class="entry-meta-categories"><i class="fa fa-folder-open-o"></i>' . get_the_category_list(', ', '') . '</span>' . "\n";
}
if ($mh_magazine_options['post_meta_comments'] === 'enable') {
echo '<span class="entry-meta-comments"><i class="fa fa-comment-o"></i>';
mh_magazine_comment_count();
echo '</span>' . "\n";
}
echo '</div>' . "\n";
}
}
}
add_action('mh_post_header', 'mh_magazine_post_meta');
How do I do this? I don't like installing plugins, so I am thinking of something I can add myself and call through a custom field.
I run a news website, and I think there are considerable benefits to adding a last date updated option to it. However, everything I see online is for replacing the published date with the date modified. I want both, like in this article by the NYT.
What's more, I don't want this on every article since I edit articles a lot and don't always want each of them to reflect it. Instead, I want it for "evergreen" content that has proven popular long after it was published; my hope is that Google shows the more recent time in search results. As a result, I prefer doing this with a custom field or a CF plugin -- I currently have CMB2 installed.
The not-very-helpful theme I use uses the below function to add dates to posts:
if (!function_exists('mh_magazine_post_meta')) {
function mh_magazine_post_meta() {
$mh_magazine_options = mh_magazine_theme_options();
if ($mh_magazine_options['post_meta_date'] === 'enable' || $mh_magazine_options['post_meta_author'] === 'enable' && in_the_loop() || $mh_magazine_options['post_meta_cat'] === 'enable' && in_the_loop() && is_singular() || $mh_magazine_options['post_meta_comments'] === 'enable') {
echo '<div class="mh-meta entry-meta">' . "\n";
if ($mh_magazine_options['post_meta_date'] === 'enable') {
echo '<span class="entry-meta-date updated"><i class="fa fa-clock-o"></i><a href="' . esc_url(get_month_link(get_the_time('Y'), get_the_time('m'))) . '">' . get_the_date() . '</a></span>' . "\n";
}
if ($mh_magazine_options['post_meta_author'] === 'enable' && in_the_loop()) {
echo '<span class="entry-meta-author author vcard"><i class="fa fa-user"></i><a class="fn" href="' . esc_url(get_author_posts_url(get_the_author_meta('ID'))) . '">' . esc_html(get_the_author()) . '</a></span>' . "\n";
}
if ($mh_magazine_options['post_meta_cat'] === 'enable' && in_the_loop() && is_singular()) {
echo '<span class="entry-meta-categories"><i class="fa fa-folder-open-o"></i>' . get_the_category_list(', ', '') . '</span>' . "\n";
}
if ($mh_magazine_options['post_meta_comments'] === 'enable') {
echo '<span class="entry-meta-comments"><i class="fa fa-comment-o"></i>';
mh_magazine_comment_count();
echo '</span>' . "\n";
}
echo '</div>' . "\n";
}
}
}
add_action('mh_post_header', 'mh_magazine_post_meta');
How do I do this? I don't like installing plugins, so I am thinking of something I can add myself and call through a custom field.
Share Improve this question edited Apr 10, 2020 at 18:11 RiddleMeThis 3,8078 gold badges22 silver badges30 bronze badges asked Apr 10, 2020 at 17:56 barney23barney23 111 bronze badge 2- WP already has a date modified field in the posts table! Also just because a post has a modified date, doesn't mean you have to display it. – Tom J Nowell ♦ Commented Apr 10, 2020 at 18:40
- When you say in the posts table, where exactly do you mean? I know it shows modified dates, and I can see this in the source code -- probably because I enabled the option in my SEO plugin. About displaying it, I made it clear in my original post that I want to enable it for certain posts, not all. That is why I am asking for a custom field option. – barney23 Commented Apr 10, 2020 at 18:47
1 Answer
Reset to default 0Adding a custom field to conditionally display last modified date:
You can use the built-in custom fields metabox of your post. Usually it is hidden by default, so you have to enable it going to 'Screen Options ' and checking the 'Custom Fields' box like this:
If you're using the new block editor, you'll find the option by going to the Edit Post Screen > Click the three dots button on the top right corner > Settings > Advanced Panels.
Now, add a new custom field, let's call it show_last_modified
and set it to true.
Displaying the last modified date:
The function the_modified_date()
will give you the date on which the post was last modified.
To display it, seems that you will have to override the mh_magazine_post_meta()
function.
function mh_magazine_post_meta() {
$mh_magazine_options = mh_magazine_theme_options();
if ($mh_magazine_options['post_meta_date'] === 'enable' || $mh_magazine_options['post_meta_author'] === 'enable' && in_the_loop() || $mh_magazine_options['post_meta_cat'] === 'enable' && in_the_loop() && is_singular() || $mh_magazine_options['post_meta_comments'] === 'enable') {
echo '<div class="mh-meta entry-meta">' . "\n";
if ($mh_magazine_options['post_meta_date'] === 'enable') {
echo '<span class="entry-meta-date updated"><i class="fa fa-clock-o"></i><a href="' . esc_url(get_month_link(get_the_time('Y'), get_the_time('m'))) . '">' . get_the_date() . '</a></span>' . "\n";
// Custom code
if(!empty(get_post_meta(get_the_ID(), 'show_last_modified', true)){
echo 'Last modified date:' . the_modified_date();
}
//
}
if ($mh_magazine_options['post_meta_author'] === 'enable' && in_the_loop()) {
echo '<span class="entry-meta-author author vcard"><i class="fa fa-user"></i><a class="fn" href="' . esc_url(get_author_posts_url(get_the_author_meta('ID'))) . '">' . esc_html(get_the_author()) . '</a></span>' . "\n";
}
if ($mh_magazine_options['post_meta_cat'] === 'enable' && in_the_loop() && is_singular()) {
echo '<span class="entry-meta-categories"><i class="fa fa-folder-open-o"></i>' . get_the_category_list(', ', '') . '</span>' . "\n";
}
if ($mh_magazine_options['post_meta_comments'] === 'enable') {
echo '<span class="entry-meta-comments"><i class="fa fa-comment-o"></i>';
mh_magazine_comment_count();
echo '</span>' . "\n";
}
echo '</div>' . "\n";
}
}
add_action('mh_post_header', 'mh_magazine_post_meta');
Place the above code on your child theme and customize the markup to suit your needs.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744348206a4569815.html
评论列表(0条)