1

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.

  • 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 Apr 10 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 Apr 10 at 18:47
0

Adding 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:

enter image description here

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.

enter image description here

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.

| improve this answer | |
  • Thanks. I've tried it, but there's no change. The date stays the same. – barney23 Apr 10 at 19:28
  • Where did you add the code? – Himad Apr 11 at 12:21
  • Child theme, in a custom functions plugin. – barney23 Apr 12 at 10:59
  • If you remove the original function and leave only the new one on the child theme, does it get displayed? – Himad Apr 12 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.