<\/figure><\/div>\n\n\n\nThat doesn't work for my site either, because it looks like I've only just published it. That'd be a bit weird with the 30+ comments on it dating back to 2019!<\/p>\n\n\n\n
That code works by showing only the updated date if it exists, otherwise showing the published date.<\/p>\n\n\n\n
The good thing about doing it that way is you don't have to mess with the CSS code to display the updated date.<\/p>\n\n\n\n
How to add text before the meta date in GeneratePress<\/h2>\n\n\n\n To add text just before the meta date, connect a function to the filter generate_inside_post_meta_item_output<\/em>. If the meta item is \"date<\/em>\", return your text.<\/p>\n\n\n\nThe code to do that is:<\/p>\n\n\n\n
add_filter ( 'generate_inside_post_meta_item_output', 'custom_date_text', 10, 2 );\n\nfunction custom_date_text( $nothing, $type ) {\n if ( 'date' !== $type ) {\n return null;\n }\n\n return \"Text before date: \";\n}<\/code><\/pre>\n\n\n\nThis will insert \"Text before date: \" (or whatever you put there) after <span class=\"posted-on\"><\/em> and before <time><\/em>.<\/p>\n\n\n\nThis will insert the same text, whether the date displayed is the published date or the updated date.<\/p>\n\n\n\n
How to display different text for published and updated dates<\/h2>\n\n\n\n If you want to display different text for the published date and updated date, use this code:<\/p>\n\n\n\n
add_filter ( 'generate_inside_post_meta_item_output', 'custom_date_text', 10, 2 );\n\nfunction custom_date_text( $nothing, $type ) {\n if ( 'date' !== $type ) {\n return null;\n }\n \n $updated_time = get_the_modified_time( 'U' );\n $published_time = get_the_time( 'U' ) + 1800;\n\n if ( $updated_time > $published_time ) {\n return \"Last Updated: \";\n }\n\n return \"Published: \";\n}<\/code><\/pre>\n\n\n\nThe code that gets and compares the $updated_time<\/em> and $published_time<\/em> is the same as GeneratePress uses for its comparison.<\/p>\n\n\n\nIf you don't want anything to display before one of them, return null<\/em> rather than false, because the function return is echoed and you'll get some weirdness.<\/p>\n\n\n\nUse this code in conjunction with add_filter( 'generate_post_date_show_updated_only', '__return_true' );<\/em> to display the updated date correctly.<\/p>\n\n\n\nThe end result is that my posts now display \"Last Updated\" and the updated date where it exists.<\/p>\n\n\n\n
<\/figure><\/div>\n\n\n\nThe published date now doesn't render in the code either.<\/p>\n\n\n\n
<\/figure><\/div>\n\n\n\nBut let's say you wanted the published date to render as well.<\/p>\n\n\n\n
Let's say you wanted to add text before both the published date and the updated date.<\/p>\n\n\n\n
Unfortunately there's no easy way to do that in GeneratePress. You have to overwrite one of their functions. This should only be done if you keep an eye on things, because it has a much greater potential to break on a later update.<\/p>\n\n\n\n
How to display published and updated date in GeneratePress with custom text<\/h2>\n\n\n\n Overwrite the generate_posted_on()<\/em> function with your own that sends the processing of the \"date<\/em>\" item to your custom function. That custom function will be a replica of generate_do_post_meta_item($item)<\/em> with your own custom text.<\/p>\n\n\n\nBoth functions are in generatepress\/inc\/structure\/post-meta.php<\/em>. You'll find them around lines 413 and 162 respectively.<\/p>\n\n\n\nI wanted to show \"Published:<\/strong> 5 July 2020\" where there was no updated date and \"Published: <\/strong>5 July 2020, Updated: <\/strong>10 April 2022\" where there was an updated date.<\/p>\n\n\n\nThe code to do all that is:<\/p>\n\n\n\n
function generate_posted_on() {\n $items = generate_get_header_entry_meta_items();\n\n foreach ( $items as $item ) {\n if ( 'date' == $item ) {\n custom_generate_do_post_meta_item( $item );\n }\n else {\n generate_do_post_meta_item( $item );\n }\n }\n}\n\nfunction custom_generate_do_post_meta_item( $item ) {\n if ( 'date' !== $item ) {\n return false;\n }\n\n $time_string = '<time class=\"entry-date published\" datetime=\"%1$s\"%5$s>%2$s<\/time>';\n\n $updated_time = get_the_modified_time( 'U' );\n $published_time = get_the_time( 'U' ) + 1800;\n $schema_type = generate_get_schema_type();\n\n if ( $updated_time > $published_time ) {\n if ( apply_filters( 'generate_post_date_show_updated_only', false ) ) {\n $time_string = '<time class=\"entry-date updated-date\" datetime=\"%3$s\"%6$s>%4$s<\/time>';\n } else {\n $time_string = '<strong>Published<\/strong>: ' . $time_string . ', <strong>Updated<\/strong>: <time class=\"updated\" datetime=\"%3$s\"%6$s>%4$s<\/time>';\n }\n } else {\n $time_string = '<strong>Published<\/strong>: <time class=\"entry-date published\" datetime=\"%1$s\"%5$s>%2$s<\/time>';\n }\n\n $time_string = sprintf(\n $time_string,\n esc_attr( get_the_date( 'c' ) ),\n esc_html( get_the_date() ),\n esc_attr( get_the_modified_date( 'c' ) ),\n esc_html( get_the_modified_date() ),\n 'microdata' === $schema_type ? ' itemprop=\"datePublished\"' : '',\n 'microdata' === $schema_type ? ' itemprop=\"dateModified\"' : ''\n );\n\n $posted_on = '<span class=\"posted-on\">%1$s%4$s<\/span> ';\n\n if ( apply_filters( 'generate_post_date_link', false ) ) {\n $posted_on = '<span class=\"posted-on\">%1$s<a href=\"%2$s\" title=\"%3$s\" rel=\"bookmark\">%4$s<\/a><\/span> ';\n }\n\n echo apply_filters(\n 'generate_post_date_output',\n sprintf(\n $posted_on,\n apply_filters( 'generate_inside_post_meta_item_output', '', 'date' ),\n esc_url( get_permalink() ),\n esc_attr( get_the_time() ),\n $time_string\n ),\n $time_string,\n $posted_on\n );\n\n}<\/code><\/pre>\n\n\n\nI know that can seem somewhat daunting, but most of it is a copy of the GeneratePress functions I mentioned above.<\/p>\n\n\n\n
There are a few lines of code you can remove if you don't intend ever using the feature, such as:<\/p>\n\n\n\n
if ( apply_filters( 'generate_post_date_show_updated_only', false ) ) {\n $time_string = '<time class=\"entry-date updated-date\" datetime=\"%3$s\"%6$s>%4$s<\/time>';\n}<\/code><\/pre>\n\n\n\nBecause if you ever did use that conditional, you'd be better off using the method I discussed earlier in this tutorial<\/a>.<\/p>\n\n\n\nThe original generate_posted_on()<\/em> function doesn't have the if\/else code in the foreach. We need it so that we can send the date items to our custom function and process everything else in the standard GeneratePress code.<\/p>\n\n\n\nUsually you can't just overwrite a function from the parent theme in the child theme, but this particular function is wrapped in if ( ! function_exists( 'generate_posted_on' ) ) {}<\/em>, which allows us to overwrite it in our child theme.<\/p>\n\n\n\nThe only other change to the code is in:<\/p>\n\n\n\n
if ( $updated_time > $published_time ) {\n if ( apply_filters( 'generate_post_date_show_updated_only', false ) ) {\n $time_string = '<time class=\"entry-date updated-date\" datetime=\"%3$s\"%6$s>%4$s<\/time>';\n } else {\n $time_string = '<strong>Published<\/strong>: ' . $time_string . ', <strong>Updated<\/strong>: <time class=\"updated\" datetime=\"%3$s\"%6$s>%4$s<\/time>';\n }\n} else {\n $time_string = '<strong>Published<\/strong>: ' . $time_string;\n}<\/code><\/pre>\n\n\n\nOriginally it was:<\/p>\n\n\n\n
if ( $updated_time > $published_time ) {\n if ( apply_filters( 'generate_post_date_show_updated_only', false ) ) {\n $time_string = '<time class=\"entry-date updated-date\" datetime=\"%3$s\"%6$s>%4$s<\/time>';\n } else {\n $time_string = '<time class=\"updated\" datetime=\"%3$s\"%6$s>%4$s<\/time>' . $time_string;\n }\n}<\/code><\/pre>\n\n\n\nI rewrote the $time_string<\/em> for updated to swap the order and add the Published and Updated text, wrapped in <strong><\/em> tags.<\/p>\n\n\n\nBecause I wanted Published: <\/strong>before the date where there was no updated date, I had to add the extra else at the end of if ( $updated_time > $published_time ){}<\/em>.<\/p>\n\n\n\nIf you add \"<strong>Published<\/strong>: \" at the initial $time_string<\/em>, before the updated\/published comparison, it will display Published<\/strong>: twice on pages that have been updated.<\/p>\n\n\n\nI hope that all makes sense. Please let me know in the comments if you'd like clarification on any of these steps.<\/p>\n","protected":false},"excerpt":{"rendered":"
I have a number of posts that get thousands of readers a month and dozens of comments. As more comments come in, I often update the content, or clarify things. Particularly if the theme or plugin they relate to changes. The way that GeneratePress is typically setup, it only displays the published date, which is<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[20],"yoast_head":"\n
How To Customize The Post Date In GeneratePress<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n