- This topic has 10 replies, 3 voices, and was last updated 1 year, 3 months ago by Rostyslav.
-
AuthorPosts
-
November 24, 2022 at 6:29 am #2431017RostyslavNovember 24, 2022 at 7:13 am #2431086DavidStaffCustomer Support
Hi there,
1. use the Block Element – Post Meta to create your meta:
https://docs.generatepress.com/article/block-element-post-meta-template/
1.1 For the reading time, just add a Headline block, withs some static text eg.
Time
and in Advanced > Addtional CSS Class(es) add:reading-time
2. Add this PHP Snippet to your site to swap that headline for an estimated reading time:
add_filter( 'render_block', function( $block_content, $block ) { if ( !is_admin() && ! empty( $block['attrs']['className'] ) && 'reading-time' === $block['attrs']['className'] ){ $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 ); $block_content = '<span class="read-time">' . $time . ' minutes</span>'; } return $block_content; }, 10, 2 );
November 24, 2022 at 7:29 am #2431105RostyslavThanks for answering so fast.
I want to keep the built-in generatepress date and author metas. Is there any code that adds the reading time next to that default metas : https://ibb.co/Y8hRQ7W
Thanks!
November 24, 2022 at 7:38 am #2431118DavidStaffCustomer SupportAre you already using any custom functions on the post meta ?
November 24, 2022 at 8:14 am #2431486RostyslavYes:
/**If you want to show the updated date if it exists, otherwise the publish date**/ add_filter( 'generate_post_date_show_updated_only', '__return_true' ); /**añadir actualizado el antes de la fecha**/ add_filter( 'generate_post_date_output','tu_add_to_post_date' ); function tu_add_to_post_date( $output ) { return '<span class="date-label">Actualizado el </span>' . $output; }
November 24, 2022 at 5:20 pm #2432054Fernando Customer SupportHi Rostyslav,
I see.
You can try adding this PHP snippet:
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; } );
Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets
November 28, 2022 at 1:59 am #2438455RostyslavHi, thanks!
How can I display just minutes without seconds?
Thanks!
November 28, 2022 at 5:21 am #2438746DavidStaffCustomer SupportChange the code to:
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 = ceil( $word_count / 200 ); if($m < 2) { $time = '1 min' } else { $time = $m . ' min'; } return $time; } add_filter( 'generate_post_author_output', function( $output ) { $output .= '<span class="read-time">Reading time: ' . tu_estimated_reading_time() . '</span>'; return $output; } );
November 28, 2022 at 12:24 pm #2439727RostyslavThanks
November 29, 2022 at 2:15 am #2440581DavidStaffCustomer SupportYou’re welcome
December 10, 2022 at 2:08 pm #2457362Rostyslavfunction 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 = ceil( $word_count / 200 ); if($m < 2) { $time = '1 min' } else { $time = $m . ' min'; } return $time; } add_filter( 'generate_post_author_output', function( $output ) { $output .= '<span class="read-time">Reading time: ' . tu_estimated_reading_time() . '</span>'; return $output; } );
Hi! That code gives me unexpected error.
Thanks
-
AuthorPosts
- You must be logged in to reply to this topic.