Site logo

Archived topic

Post Visit Counter and Mins Read time

42 replies · Started by Sourabh on December 3, 2020

Viewing posts 16–30 of 43

Here's the complete 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 );
    
    if ($time == 1) {
        return $time . ' minute';
    } else {
        return $time . ' minutes';
    }
}

add_filter( 'generate_post_author_output', function( $output ) {
    $output .= '<div class="read-time">Reading time: ' . tu_estimated_reading_time() . '</div>';

    return $output;
} );

Give this a shot:

.read-time {
    display: inline-block;
    border-left: 1px solid #ddd;
    padding-left: 5px;
    margin-left: 3px;
}

Hi Thanks Tom,

Above CSS worked only for reading time (As all in a row now), thanks for that, But I requested CSS for the entire Meta Like below format-

Author By: | Date | Comments | Reading Time | …

(Good if also getting one line up and one line down, it’s like two lines box), give a CSS for arranging this.

Help if you can please.

Yes Tom,

My site is broken again, It's happening because of the child theme, Don't know whats wrong, (I have raised a separate ticket for my child theme concern), Please review that, so that i will restore it again from backup...

however a meta is a concern, I have already read this article it's about metadata, however, I requested custom CSS for the entire Meta display in the below format-

Author By: | Date | Comments | Reading Time | …

(Good if also getting one line up and one line down, it’s like two lines box), give a CSS for arranging this.

https://www.dropbox.com/s/rc7251uh61goazx/Meta%20below%20title.png?dl=0

Let's figure out the error first, then we can re-open this one and provide some CSS.

Yes I like your decision..

HI Tom,

the issue has been resolved so Let's finish this topic and give me CSS for the below -

Author By: | Date | Comments | Reading Time | …

(Good if also getting one line up and one line down, it’s like two lines box), give a CSS for arranging this.

We'll need to change it up to rearrange where the reading time goes. This is the updated 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 );
    
    if ($time == 1) {
        return $time . ' minute';
    } else {
        return $time . ' minutes';
    }
}

add_action( 'generate_post_meta_items', function( $item ) {
    if ( 'reading-time' === $item ) {
        echo '<span class="reading-time">Reading time: ' . tu_estimated_reading_time() . '</span>';
    }
} );

add_filter( 'generate_header_entry_meta_items', function() {
    return array(
        'author',
        'date',
        'comments-link',
        'reading-time',
    );
} );

add_action( 'wp', function() {
    if ( is_single() ) {
        add_filter( 'generate_show_comments', '__return_true' );
    }
} );

More info on this method here: https://docs.generatepress.com/article/generate_header_entry_meta_items/

As for the lines, try this:

.entry-header .entry-meta {
    border-top: 1px solid #000;
    border-bottom: 1px solid #000;
    padding: 5px 0;
    margin-top: 10px;
}

Nah, Something went wrong seems -

Tom, Reading time + Comments section is not visible now, they vanished post updating the code, should i jump to the previous one ??

I think I have a few codes which Leo has given to me previously as below -

add_action( 'wp', function() {
if ( is_single() ) {
add_filter( 'generate_show_comments', '__return_true' );
}
} );

add_filter( 'generate_header_entry_meta_items', function() {
return array(
'categories',
'date',
'author',
);
} );

However, when I delete these codes from the child theme then to it's looking like this (Refer private section),

Guide me on what to do now from here ..

This archived topic is closed to new replies.