- This topic has 33 replies, 5 voices, and was last updated 5 years, 5 months ago by Leo.
-
AuthorPosts
-
August 12, 2018 at 10:12 am #646411TomLead DeveloperLead Developer
Nothing shows up at all?
Can you add this:
var_dump( $time );
After this:
$time = ceil( $word_count / $wpm );
Just to debug ๐
August 12, 2018 at 5:11 pm #646582ZadJust added it!
August 12, 2018 at 8:09 pm #646631TomLead DeveloperLead DeveloperDid it output any data on the front end of your website? I’m not seeing anything on your site, but it should at least output an empty variable. Is the code still there?
August 12, 2018 at 8:15 pm #646634ZadThis is the code I have inserted and activated on the site. As far as I know, I don’t see anything on the front end of the site,
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 ); var_dump( $time ); return $time; } add_filter( 'generate_post_author_output', function( $output ) { $output .= '<div class="read-time">Reading time: ' . tu_estimated_reading_time() . '</div>'; return $output; } );
August 12, 2018 at 8:41 pm #646642TomLead DeveloperLead DeveloperAre you using another function to modify the author as well? I assume you want this to show under the author/date?
August 12, 2018 at 8:43 pm #646643ZadYes, using the code for the gravatar entry meta
add_filter( 'generate_post_author', '__return_false' ); add_filter( 'generate_show_comments', '__return_false' ); add_filter( 'generate_post_date_output', 'tu_fancy_byline' ); function tu_fancy_byline( $date ) { printf( ' <span class="byline">%1$s</span>', sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%4$s<a href="%1$s" title="%2$s" rel="author"><span class="author-name" itemprop="name">%3$s</span></a></span>', esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ), esc_attr( sprintf( __( 'View all posts by %s', 'generatepress' ), get_the_author() ) ), esc_html( get_the_author() ), get_avatar( get_the_author_meta( 'ID' ) ) ) ); if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) { echo '<span class="comments-link">'; comments_popup_link( __( 'Leave a comment', 'generatepress' ), __( '1 Comment', 'generatepress' ), __( '% Comments', 'generatepress' ) ); echo '</span>'; } echo $date; }
And yes, it would hopefully be below that
August 12, 2018 at 8:45 pm #646644TomLead DeveloperLead DeveloperIn that case, we’d need to change:
generate_post_author_output
To:
generate_post_date_output
In your second function.
That way the reading time should be appended to the updated date, and then placed on its own line with the
<div>
element.August 12, 2018 at 8:48 pm #646646ZadIt works!! Any way to put minutes next to the output? And do I need to remove the debugging code?
August 12, 2018 at 8:49 pm #646647Zadwoops, just noticed, it says “float3” next to the date
August 13, 2018 at 9:59 am #647138TomLead DeveloperLead DeveloperThis would be your final code:
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 ); 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; } );
August 13, 2018 at 10:43 am #647165ZadIt worked!!!!!!!!! Thanks so much!!!
August 13, 2018 at 5:36 pm #647412TomLead DeveloperLead DeveloperNo problem ๐
August 13, 2018 at 5:48 pm #647415ZadJust one last thing, I was wondering if it was possible to add a few spaces to the read time for the archives page because it doesn’t look centered?
That or getting rid of the gravatar image from the gravatar entry meta function, as I don’t really need the gravatar image, but simply wanted the name to be to the left of the date.
August 13, 2018 at 6:10 pm #647421ZadNever mind, edited the CSS to get it to work!
November 5, 2018 at 6:48 pm #719986abacusI already have an Estimated Reading Time plug-in. Can I show Estimated Reading Time using Elements after title? I played around a little but no luck so far.
-
AuthorPosts
- The topic ‘Creating an Estimated Reading Time’ is closed to new replies.