Archived topic
Reading time meta in page header
3 replies · Started by Bogdan on August 7, 2020
Hello,
I've been using this snippet to calculate the reading time for every article on my site and display it as meta information next to the date and author name:
function tu_estimated_reading_time() {
$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 . ' minutes';
}
add_filter( 'generate_post_date_output', function( $output ) {
$output .= '<div class="read-time">Reading time: ' . tu_estimated_reading_time() . '</div>';
return $output;
} );
It was working perfectly until I changed the design and added the meta to the page header using elements. My Header element now looks like this:
<h1>{{post_title}}</h1>
<br>
<br>
<br>
by {{post_author}} | {{post_date}} | {{post_terms.category}}
It's a gorgeous look, but I'm missing the reading time.
How can I add it inside the header element?
Hi there,
the easiest fix is to also register the function as a shortcode like so:
add_shortcode('reading-time', 'tu_estimated_reading_time');
Then you can add it your header element using [reading-time]
Thanks David! That worked like a charm. It now looks exactly like I wanted
Glad to hear that.