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 badges1 Answer
Reset to default 1I 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
版权声明:本文标题:the content - Undefined variable error in new function 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745055690a2639919.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论