Home › Forums › Support › Trigger “Last Updated Date” by pasting shortcode in the body of an article
- This topic has 15 replies, 4 voices, and was last updated 2 years, 11 months ago by
David.
-
AuthorPosts
-
February 21, 2023 at 2:22 am #2540898
registriran
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.
February 21, 2023 at 4:39 am #2541049David
StaffCustomer SupportHi 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 theUpdatedpost 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 –> UpdatedBy 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 ?
February 22, 2023 at 5:15 am #2542461registriran
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.
February 22, 2023 at 7:06 am #2542577David
StaffCustomer SupportThe alternative is to use a custom field or custom taxonomy to “select” the option.
Are you familiar with ACF or other Custom Fields ?February 22, 2023 at 8:35 am #2542858registriran
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 thehas_tagone?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.
February 22, 2023 at 10:06 am #2542971David
StaffCustomer SupportYou can try this snippet:
add_filter( 'generate_post_date_show_updated_only', function(){ if ( has_tag('updated') ) { return true; } return false; } );February 23, 2023 at 12:04 am #2543566registriran
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!!!
February 23, 2023 at 12:45 am #2543604Fernando Customer Support
Hello there,
It’s possible. For reference, can you provide a post that has the tag updated/mild?
February 23, 2023 at 1:23 am #2543640registriran
Here it is!
February 23, 2023 at 1:25 am #2543643Fernando Customer Support
Can you try adding this through Appearance > Customize > Additional CSS?:
time.entry-date.updated-date::before { content: "Last Updated: "; }February 23, 2023 at 1:33 am #2543656registriran
Did the trick, Fernando.
Thank you to both, this solution to my very specific issue has earned GP Support a 5-star review.
February 23, 2023 at 1:37 am #2543661Fernando Customer Support
You’re welcome!
March 10, 2023 at 6:44 pm #2563613Kelsey
So, if I wanted to replicate this outcome, would I need to still add a tag called “updated?”
March 11, 2023 at 6:42 am #2563960David
StaffCustomer SupportHi there,
if you use this snippet:
Then yes the post would need an updated tag.
March 24, 2023 at 10:20 am #2580789registriran
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.
-
AuthorPosts
- You must be logged in to reply to this topic.