Php 字数及;我可以读取时间错误';t在Wordpress中跟踪和修复

Php 字数及;我可以读取时间错误';t在Wordpress中跟踪和修复,php,wordpress,Php,Wordpress,使用Wordpress,我改变了一个自定义主题,用从网站上找到的脚本显示文章字数和估计阅读时间。我甚至添加了一个选项来打开/关闭它。剧本写得很好,至少我是这么想的 当我打开Wordpress调试选项来查找主题中的错误时,我为这个脚本得到了一个错误。然而,我不认为有什么问题 查看网页时显示的错误: 这篇文章包含 注意:未定义变量:post in /home/xxxxxx/wordpress.xxxxxx.com/wp-content/themes/xxxxxx/functions.php 在线16

使用Wordpress,我改变了一个自定义主题,用从网站上找到的脚本显示文章字数和估计阅读时间。我甚至添加了一个选项来打开/关闭它。剧本写得很好,至少我是这么想的

当我打开Wordpress调试选项来查找主题中的错误时,我为这个脚本得到了一个错误。然而,我不认为有什么问题

查看网页时显示的错误

这篇文章包含

注意:未定义变量:post in /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个字

这篇文章会带你四处转转

注意:未定义变量:post in /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;
}
    /* 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')
        )
    ));
<?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>';
    }
?>
我在:/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;
}
    /* 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')
        )
    ));
<?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>';
    }
?>
在主题模板文件中:/template parts/content.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;
}
    /* 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')
        )
    ));
<?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>';
    }
?>

我不认为错误在于我添加的选项,但我在这里添加了它以防万一。我认为错误在于我复制的剧本本身。它是什么?我如何修复它?

试试这个:

/*** 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
时它在范围内