Site logo

Archived topic

Add Reading time on posts after the author name

10 replies · Started by Rostyslav on November 24, 2022

Viewing posts 1–11 of 11

Hello!

How can I add a Reading time on posts after the author name?

https://ibb.co/Y8hRQ7W

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 );

Thanks 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!

Are you already using any custom functions on the post meta ?

Yes:

/**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;
}

Hi 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

Hi, thanks!

How can I display just minutes without seconds?

Thanks!

Change 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;
} );

Thanks

You're welcome

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;
} );

Hi! That code gives me unexpected error.

Thanks

This archived topic is closed to new replies.