- This topic has 7 replies, 3 voices, and was last updated 2 years, 6 months ago by
Ying.
-
AuthorPosts
-
August 25, 2022 at 6:45 am #2323475
Mathieu
Hi there,
I’m trying to add at the beginning of my blog content the following informations: “Updated date + Published date + reading time + Author”, exactly as shown in this topic.
For this reason, I got the following code that I added to Code Snippets :
add_filter( 'generate_post_date_output', function( $output, $time_string ) { $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">Published on %2$s</time>'; if ( get_the_date() !== get_the_modified_date() ) { $time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">Updated: %4$s</time> <time class="entry-date published" datetime="%1$s" itemprop="datePublished">Published: %2$s</time>'; } $time_string = sprintf( $time_string, esc_attr( get_the_date( 'c' ) ), esc_html( get_the_date() ), esc_attr( get_the_modified_date( 'c' ) ), esc_html( get_the_modified_date() ) ); return sprintf( '<span class="posted-on">%s</span> ', $time_string ); }, 10, 2 ); function tu_estimated_reading_time() { $post = get_post(); $content = $post->post_content; $wpm = 300; // How many words per minute. $clean_content = strip_shortcodes( $content ); $clean_content = strip_tags( $clean_content ); $word_count = str_word_count( $clean_content ); $time = ceil( $word_count / $wpm ); return $time . ' mins'; } add_filter( 'generate_post_date_output', function( $output ) { $output .= '~' . tu_estimated_reading_time() . ' to read'; return $output; } );
But then I’m getting lost : nothing is displayed on my articles.
How to indicate that the PHP code must be displayed at this precise place ?Should I use “elements” >> Hook ?
Many thanks !
MathieuAugust 25, 2022 at 8:45 am #2323732David
StaffCustomer SupportHi there,
that code has to be added using one of the method in this doc:
https://docs.generatepress.com/article/adding-php/
TLDR: Are you using a Child Theme?
If Yes, then the code goes in your Child Theme functions.php file
If No, then install the Code Snippets plugin and add the code to a New code snippet.August 25, 2022 at 8:52 am #2323738Mathieu
Hi David,
Thanks for your answer.
Code has been added using “Code Snippets plugin” as recommanded in your methodI’m not using any child theme.
Please check screenshot in private information if you want to checkThanks !
August 25, 2022 at 9:14 am #2323758David
StaffCustomer SupportAah sorry – i should have checked your URL.
Can you remove that PHP Code as that will only apply to the standard theme templates.
Then:1. Add this PHP Snippet to create a Shortcode called
[tu_reading_time]
which can be used to display the Reading Time.function tu_estimated_reading_time() { ob_start(); $post = get_post(); $content = $post->post_content; $wpm = 300; // How many words per minute. $clean_content = strip_shortcodes( $content ); $clean_content = strip_tags( $clean_content ); $word_count = str_word_count( $clean_content ); $time ='<div class="read-time"><span class="gp-icon">•</span>Reading time: '. ceil( $word_count / $wpm ) . ' minutes'. '</div>'; return $time; return ob_get_clean(); } add_shortcode('tu_reading_time', 'tu_estimated_reading_time');
2. Go to Appearance > Elements and edit the Element titled:
Single Post Hero
2.1 You need to add a Headline Block, for each of the dynamic data options. Published Date, Updated Date and Author. And i1 more for the read time which you will add the
[tu_reading_time]
shortcode,August 25, 2022 at 9:42 am #2323801Mathieu
Thanks David !
Works great, i can get all datas i wanted. Only thing is that shortcode for “reading time” do not use same CSS, so font-size for example is different.How am i suppose to have the same font-size for “reading time” than “update or publish date” ?
(sent you the link on private info)August 25, 2022 at 11:23 am #2323892Ying
StaffCustomer SupportHi Mathieu,
You can select the container block and set the font size there, so the text inside the container can inherit the font size from the container.
August 25, 2022 at 2:58 pm #2324003Mathieu
Thanks David, thanks Ying for your precious help 🙂
Best support ever !August 25, 2022 at 3:15 pm #2324011Ying
StaffCustomer SupportYou are welcome 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.