- This topic has 16 replies, 2 voices, and was last updated 3 years, 3 months ago by
Fernando.
-
AuthorPosts
-
December 11, 2022 at 5:15 pm #2458343
Michelle
I am using php to add an estimated reading time to my single post meta. I want to add it to my blog page as well so people can see at a glance how long an article will take to read. Problem is, the estimator on the blog page only considers the excerpt length, not the entire article.
How can I display the estimated time from my Single Posts on my blog page?
December 11, 2022 at 6:08 pm #2458360Michelle
I *could* create an ACF, add it into Elements, create a place with a Headline block where I could manually add the time in the post editor directly after publishing the post and seeing what the estimated reading time says on the single post page, but that seems like a “fix it with tape” kind of solution.
December 11, 2022 at 7:09 pm #2458378Fernando Customer Support
Hi Michelle,
Can you provide the link to your Blog page? I’ll check how it’s currently set up.
You may use the Private Information field for this: https://docs.generatepress.com/article/using-the-premium-support-forum/#private-information
December 11, 2022 at 9:49 pm #2458447Michelle
Thanks. Yes, here is my Blog page. (It’s under maintenance, so you’ll have to log in.) My credentials are below.
December 11, 2022 at 10:41 pm #2458462Fernando Customer Support
That’s odd. Your code should take into account the entire article.
Can you try this code instead to test?:
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 ); $m = floor($word_count / 200); $s = floor($word_count % $wpm / ($wpm / 60)); if($m < 1) { $time = $s . ' second' . ($s <= 1 ? '' : 's'); } else { $time = $m . ' minute' . ($m <= 1 ? '' : 's') . ', ' . $s . ' second' . ($s <= 1 ? '' : 's'); } return $time; } add_filter( 'generate_post_author_output', function( $output ) { $output .= '<span class="read-time">Reading time: ' . tu_estimated_reading_time() . '</span>'; return $output; } );Let us know how it goes.
December 12, 2022 at 1:40 pm #2459611Michelle
I put the new code in and found that it doesn’t pull up the time; It just leaves the shortcode [reading-time]…
December 12, 2022 at 7:19 pm #2459789Fernando Customer Support
Oh. I see what’s the issue. Sorry, I didn’t notice you’re using a static page for your Blog Page.
You need to add the estimated time differently to make it work.
First, instead of a Shortcode Block, add a GB Headline Block instead.
Give it a text of
Placeholderand a classcu-estimated-time.Adding Custom Classes: https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/
Then, add this Snippet:
function tu_estimated_reading_timeb() { $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 ); $m = floor($word_count / 200); $s = floor($word_count % $wpm / ($wpm / 60)); if($m < 1) { $time = $s . ' second' . ($s <= 1 ? '' : 's'); } else { $time = $m . ' minute' . ($m <= 1 ? '' : 's') . ', ' . $s . ' second' . ($s <= 1 ? '' : 's'); } return $time; } add_filter( 'render_block', function( $block_content, $block ) { if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'cu-estimated-time' ) !== false ) { $myreplace1 = 'Placeholder'; $myinsert1 = tu_estimated_reading_timeb(); $block_content = str_replace( $myreplace1, $myinsert1 , $block_content ); } return $block_content; }, 10, 2 );Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets
Let us know how it goes.
December 13, 2022 at 12:45 pm #2460887Michelle
Hello. I added the text and class, plus the code snippet and received this message:
Don’t Panic
The code snippet you are trying to save produced a fatal error on line 73:is_readable(): open_basedir restriction in effect. File(/dev/urandom) is not within the allowed path(s)
December 13, 2022 at 6:06 pm #2461058Fernando Customer Support
That’s odd. Can I try to add the code myself to your site so I can see the error firsthand?
December 13, 2022 at 7:15 pm #2461091Michelle
Yes, go ahead.
December 13, 2022 at 7:35 pm #2461103Fernando Customer Support
I was able to add it in Code Snippets.
Now, just add a GB Headline Block with text
Placeholderand a classcu-estimated-time. Example: https://share.getcloudapp.com/WnuZJZkXDecember 13, 2022 at 7:49 pm #2461112Michelle
That worked! Now all that’s left is making the single post page estimated times match the time stated on the blog page.
December 13, 2022 at 8:05 pm #2461117Fernando Customer Support
Which format do you prefer – the one currently on the Blog Page or the one on the Single Post Pages?
December 13, 2022 at 8:19 pm #2461123Michelle
I like the format on the single post pages the best – “4 min read”. Most important though is that the estimated times agree. My current post says 4 minutes and 52 seconds on the blog page, and 4 minutes on the single post page.
December 13, 2022 at 8:32 pm #2461131Fernando Customer Support
I see. Replace the code I added in Code Snippets with this:
function tu_estimated_reading_timeb() { $post = get_post(); $content = $post->post_content; $wpm = 250; // 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 . ' min read'; } add_filter( 'render_block', function( $block_content, $block ) { if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'cu-estimated-time' ) !== false ) { $myreplace1 = 'Placeholder'; $myinsert1 = tu_estimated_reading_timeb(); $block_content = str_replace( $myreplace1, $myinsert1 , $block_content ); } return $block_content; }, 10, 2 );That should make them identical.
-
AuthorPosts
- You must be logged in to reply to this topic.