• Resolved dhyanakendra

    (@dhyanakendra)


    Hi

    Estimated read time is working fine after the last resolution provided by Tom(), however, I am unable to fine tune it better. Let me explain.

    Background:
    Our main posts are in English. But we want to provide manual translations for 6 more languages. So far so good.

    Issue:
    Estimated Read Time takes into account all the translated text instead of the original English one. This increases the Estimated Read time Value

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

    $text = ‘ minutes’;

    if ( 1 == $time ) {
    $text = ‘ minute’;
    }

    return $time . $text;
    }

    add_filter( ‘generate_post_date_output’, function( $output ) {
    $output .= ‘<div class=”read-time”>Reading time: ‘ . tu_estimated_reading_time() . ‘</div>’;

    return $output;
    } );

    Thanks in advance!

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Theme Author Tom

    (@edge22)

    Hi there,

    Just so I understand correctly, the code is calculating the post content length based on the content of all the languages, instead of just one?

    Thread Starter dhyanakendra

    (@dhyanakendra)

    Hi Tom

    Absolutely Yes. I want the estimated reading time to show only the value for English language and not for others.

    Theme Author Tom

    (@edge22)

    Hmm, well it gets the content using this:

    $content = $post->post_content;

    I’m not sure how to get only the English content, unfortunately.

    One option is to manually set the word count in a custom field, then do this:

    $words = get_post_meta( get_the_ID(), 'your_word_count_field', true );
    
    if ( $words ) {
        $content = $words;
    } else {
        $content = $post->post_content;
    }

    Let me know if that makes sense or not ??

    Thread Starter dhyanakendra

    (@dhyanakendra)

    Hi Tom
    I am marking this as resolved since the issue is not that important. Thanks again for your expert advice and support, as always

    Theme Author Tom

    (@edge22)

    No problem!

    Let me know if you ever want to revisit it ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Estimated Reading Time’ is closed to new replies.