admin管理员组

文章数量:1406920

I wanted make function to count the time last the user read the article but after putting it in fuctions.php I see this error:

Notice: Undefined variable: post in /home/karnetac/public_html/wp-content/themes/karneta/functions.php on line 2 Notice: Trying to get property 'ID' of non-object in /home/karnetac/public_html/wp-content/themes/karneta/functions.php on line 2

my codes:

function sh_reading_time() {
    $content = get_post_field( 'post_content', $post->ID );
    $word_count = str_word_count( strip_tags( $content ) ); 
    $readingtime = ceil($word_count / 200);

    return $readingtime;
}

would you plese help me? I think it is easy but I can not fix it now. thanks

I wanted make function to count the time last the user read the article but after putting it in fuctions.php I see this error:

Notice: Undefined variable: post in /home/karnetac/public_html/wp-content/themes/karneta/functions.php on line 2 Notice: Trying to get property 'ID' of non-object in /home/karnetac/public_html/wp-content/themes/karneta/functions.php on line 2

my codes:

function sh_reading_time() {
    $content = get_post_field( 'post_content', $post->ID );
    $word_count = str_word_count( strip_tags( $content ) ); 
    $readingtime = ceil($word_count / 200);

    return $readingtime;
}

would you plese help me? I think it is easy but I can not fix it now. thanks

Share Improve this question asked Oct 24, 2019 at 15:27 Sh.DehnaviSh.Dehnavi 1093 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I believe that's telling you that $post (in $post->ID) is undefined. Try this instead:

function sh_reading_time() {
    $content = get_post_field( 'post_content', get_the_ID() );
    $word_count = str_word_count( strip_tags( $content ) ); 
    $readingtime = ceil($word_count / 200);

    if ($readingtime == 1) {
      $timer = " minute";
    } else {
      $timer = " minutes";
    }
    $totalreadingtime = $readingtime . $timer;

    return $totalreadingtime ;
}

本文标签: the contentUndefined variable error in new function