我使用Wordpress,更改了一个自定义主题,以显示从网站上找到的脚本的帖子数和估计的阅读时间。我什至还添加了一个选项来打开/关闭它。脚本工作正常...,或者我想。
当我打开Wordpress调试选项以查找主题错误时,此脚本得到了一个提示。但是,我看不到问题。
查看网页时显示的错误:
此帖子包含
注意:未定义变量:发布于 /home/xxxxxx/wordpress.xxxxxx.com/wp-content/themes/xxxxxx/functions.php 在1676行
注意:尝试获取非对象的属性 /home/xxxxxx/wordpress.xxxxxx.com/wp-content/themes/xxxxxx/functions.php 在1676行
52个字。
这篇文章将带您了解
注意:未定义变量:发布于 /home/xxxxxx/wordpress.xxxxxx.com/wp-content/themes/xxxxxx/functions.php 在1683行
注意:尝试获取非对象的属性 /home/xxxxxx/wordpress.xxxxxx.com/wp-content/themes/xxxxxx/functions.php 在1683行
1分钟阅读。
我复制并添加到以下脚本的脚本:/functions.php
/*** ADD POST WORD COUNT ***/
function word_count() {
$content = get_post_field( 'post_content', $post->ID );
$word_count = str_word_count( strip_tags( $content ) );
return $word_count;
}
/*** ADD ESTIMATED READING TIME ***/
function reading_time() {
$content = get_post_field( 'post_content', $post->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;
}
我在/functions.php
中添加的选项 /* MAIN SETTINGS - Show Entry Word Count Setting */
$wp_customize->add_setting('swag_main_post_word_count', array(
'default' => 'swag-main-post-word-count-yes'
));
$wp_customize->add_control('swag_main_post_word_count', array(
'label' => 'Display Post Word Count',
'section' => 'swag_main_design',
'type' => 'select',
'choices' => array(
'swag-main-post-word-count-yes' => __('Yes'),
'swag-main-post-word-count-no' => __('No')
)
));
/* MAIN SETTINGS - Show Entry Reading Time Setting */
$wp_customize->add_setting('swag_main_post_read_time', array(
'default' => 'swag-main-post-read-time-yes'
));
$wp_customize->add_control('swag_main_post_read_time', array(
'label' => 'Display Post Read Time',
'section' => 'swag_main_design',
'type' => 'select',
'choices' => array(
'swag-main-post-read-time-yes' => __('Yes'),
'swag-main-post-read-time-no' => __('No')
)
));
在主题模板文件中:/template-parts/content.php
<?php
if (isset($swag_main_post_word_count) && $swag_main_post_word_count=='swag-main-post-word-count-yes') {
echo '<div class="word-count">This post contains ';
echo '' . word_count() . '';
echo ' words.</div>';
}
?>
<?php
if (isset($swag_main_post_read_time) && $swag_main_post_read_time=='swag-main-post-read-time-yes') {
echo '<div id="reading-time">This post will take you about ';
echo '' . reading_time() . '';
echo ' to read.</div>';
}
?>
我不认为问题在于添加的选项,但是为了以防万一,我在这里添加了这些选项。我认为问题出在我自己复制的脚本中。这是什么,我该如何解决?
答案 0 :(得分:1)
尝试一下:
/*** ADD POST WORD COUNT ***/
function word_count() {
global $post;
$content = get_post_field( 'post_content', $post->ID );
$word_count = str_word_count( strip_tags( $content ) );
return $word_count;
}
/*** ADD ESTIMATED READING TIME ***/
function reading_time() {
global $post;
$content = get_post_field( 'post_content', $post->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;
}
我已将global $post;
添加到每个函数中,因此在调用$post->ID
时它在范围内。