Site logo

Archived topic

Trigger "Last Updated Date" by pasting shortcode in the body of an article

15 replies · Started by registriran on February 21, 2023

Viewing posts 1–15 of 16

Hello,

Can I trigger a hook element by pasting a shortcode in the body of a single post? I'm not code-savvy so feel free to offer me a better way to do the following if there's one.

What do I want: I want to create something like this about my Last Updated Date: https://docs.generatepress.com/article/block-element-post-meta-template/ ...

However, I don't want it to appear sitewide instantly - I only want to replace the published with an updated date AFTER I've actually significantly updated a post based on my judgment (and not just changed a single comma as is with the automated solution...).

So I would like to update an old post and then, if I decide, just add a shortcode somewhere in the body of the article, triggering the new hook and replacing the default Published Date with the Last Updated On one.

Thanks in advance.

Hi there,

checking whether the post contains a shortcode or some other content is possible, but it means having to parse the posts content on every load, which isn't the most efficient method.

A real simple method would be to use a taxonomy.
E.g create a Post Tag called: Updated
Any post you want to show the updated date can be given the Updated post tag.

Then create 2 elements:

#1. With the published date. Set the Display Rules:
Location -> Posts --> All Posts
Exclusions -> Post Tag --> Updated

#2. Withe Updated Date. Set the Display Rules:
Location -> Post Tag --> Updated

By using a taxonomy you can check and uncheck this tag from the editor or admin screen. And you can also view the Post Tags admin to see all posts that have been flagged as Updated.

Would that work for you ?

Hi again,

This seems like an interesting workaround but one potential issue I see is that the name of the tag is filled in under "keywords" in my Schema, which I think Yoast outputs (not sure, could it be the theme outputting the schema for this?).

I don't want it like that because this may or may not affect the overall SEO.

Any ideas?

Thanks in advance!

Edit: I've added my website for you to check.

The alternative is to use a custom field or custom taxonomy to "select" the option.
Are you familiar with ACF or other Custom Fields ?

Ok, I was digging around and multiple issues arose:

1. If I do it with the block element method as described by Leo in the video and click the "Replace default primary post meta" then the new author's link does not get all metadata markups like itemprop, rel="author", etc. This is bad for obvious reasons, I think.

2. So then I decided to try and remove the date from the default primary post meta, only leaving the author's name with its metadata markups and then displaying dates through the block elements. I couldn't find out how to display both the date and the author's name on the same line. I don't want this because it pushes the main content further down (SEO...).

3. After some more digging I found this resource of yours: https://docs.generatepress.com/article/generate_post_date_output/

So adding

add_filter( 'generate_post_date_show_updated_only', '__return_true' );

would replace the published date on ALL posts with the updated date only. But evidently, custom PHP could only be added through the Child theme, etc, and not through a hook element.

So to combine this method with the logic of your workaround I researched a little more and found this thread: https://generatepress.com/forums/topic/adding-custom-php-through-a-hook/ where Leo suggests modifying the code and using the conditional tags from WordPress.

Reading through the list of conditional tags (https://codex.wordpress.org/Conditional_Tags#A_PAGE_Page), I found the one that seemed most appropriate:

has_tag( 'mild' )

where "mild" is the name of the tag (which in our case would be "updated").

So since I'm not a programmer, do you think you could "modify" the 'generate_post_date_show_updated_only', '__return_true' code with the has_tag one?

And do you think that it would work the way I want it to:

I load the new code through the Child theme and whenever I place a single post in the "updated" tag it would only display the updated date?

Thanks in advance.

You can try this snippet:

add_filter( 'generate_post_date_show_updated_only', function(){
    if ( has_tag('updated') ) {
        return true;
    }
    return false;
} );

Hi,

It seems to work! So thank you so much!

Btw can I add text before the date? I want to add: "Last Updated: " right before the date.

Either way, thank you for working through this with me!!!

Hello there,

It's possible. For reference, can you provide a post that has the tag updated/mild?

Here it is!

Can you try adding this through Appearance > Customize > Additional CSS?:

time.entry-date.updated-date::before {
    content: "Last Updated: ";
}

Did the trick, Fernando.

Thank you to both, this solution to my very specific issue has earned GP Support a 5-star review.

You're welcome!

So, if I wanted to replicate this outcome, would I need to still add a tag called "updated?"

Just and update for anyone reading this in the future:

Google picks up the "Last Updated" date I trigger with the trick described above on each post I update and shows it in the search results.

About the tag link:

I disabled "Display post tags" from Customize => Layout => Blog => switch tabs from "Archives" to "Single" and there you can see the checkbox.

Amazingly this not only hides the tag link with CSS but actually removes it from the page, so you don't end up "spilling link juice" if you believe in that sort of thing.

So all in all, for now it works as intended.

This archived topic is closed to new replies.