尝试在Comment-template.php中获取非对象通知的属性

时间:2016-07-17 作者:Omid Toraby

我需要对WORDPRESS评论区进行一些自定义,因此我在我的子主题中使用了以下代码:

    <div id="comments" class="x-comments-area">

    <?php if ( have_comments() ) : ?>

    <?php
    //The author of current post
    $author_ID = get_the_author_meta("ID");
    //The current post ID
    $p_ID = get_the_ID();
    ?>

    <h2 class="h-comments-title"><span><?php _e( \'Comments\' , \'__x__\' ); ?>  <small>
    <?php 
    //Number of guest comments
    echo ztjalali_persian_num(number_format_i18n(count(get_comments(array(\'post_id\' => $p_ID,\'author__not_in\' => array($author_ID))))));
 ?></small></span></h2>
    <ol class="x-comments-list">
    <?php
    wp_list_comments( array(
    \'callback\' => \'x_icon_comment\',
    \'style\'    => \'ol\'
  ) );
  ?>
    </ol>

    <?php if ( get_comment_pages_count() > 1 && get_option( \'page_comments\' ) ) : ?>
    <nav id="comment-nav-below" class="navigation" role="navigation">
    <h1 class="visually-hidden"><?php _e( \'Comment navigation\', \'__x__\' ); ?></h1>
    <div class="nav-previous"><?php previous_comments_link( __( \'&larr; Older Comments\', \'__x__\' ) ); ?></div>
    <div class="nav-next"><?php next_comments_link( __( \'Newer Comments &rarr;\', \'__x__\' ) ); ?></div>
    </nav>
    <?php endif; ?>

    <?php if ( ! comments_open() && get_comments_number() ) : ?>
    <p class="nocomments"><?php _e( \'Comments are closed.\' , \'__x__\' ); ?></p>
    <?php endif; ?>

    <?php endif; ?>

    <?php
//Loading of smilies
    ob_start(); cs_print_smilies(); $cs_print_smilies = ob_get_clean();
//Comment form re-arrangement
    comment_form( array(
    \'comment_notes_before\' => \'\',
    \'comment_notes_after\'  => \'\',
    \'id_submit\'            => \'entry-comment-submit\',
    \'label_submit\'         => __( \'Submit\' , \'__x__\' ),
    \'title_reply\'          => __( \'<span>Leave a Comment</span>\' , \'__x__\' ),
    \'fields\'               => array(
    \'author\' =>
    \'<p class="comment-form-author">\' .
    \'<input id="author" name="author" type="text" value="\' . get_comment_author()  . \'" placeholder="\' . __( \'Name *\', \'__x__\' ) . \' \' . \'" size="30"/>\' .
    \'</p>\',
    \'email\'  =>
    \'<p class="comment-form-email">\' .

      \'<input id="email" name="email" type="email" aria-required="true" aria-invalid="false" value="\' .get_comment_author_email() . \'" placeholder="\' . __( \'Email *\', \'__x__\' ) . \' \' . \'" size="30"/>\' .
    \'</p>\',
    \'url\'    =>
    \'<p class="comment-form-url">\' .

      \'<input id="url" name="url" type="text" value="\' . get_comment_author_url() . \'" placeholder="\' . __( \'URL\', \'__x__\' ) . \'" size="30" />\' .
    \'</p>\'
),
    \'comment_field\' => \'<p class="comment-form-comment">\' .
                     \'</br>\'.
                      $cs_print_smilies .
                     \'<textarea id="comment" name="comment" cols="45" rows="4" placeholder="\' . _x( \'Comment *\', \'noun\', \'__x__\' ) . \'" aria-required="true"></textarea>\' .
                   \'</p>\'

) );
?>

    </div>
但通过使用上述代码,我将在我发布的帖子中的评论区上方发布这些PHP通知:

Notice: Trying to get property of non-object in /home/foo/public_html/wp-includes/comment-template.php on line 28

Notice: Trying to get property of non-object in /home/foo/public_html/wp-includes/comment-template.php on line 46

Notice: Trying to get property of non-object in /home/foo/public_html/wp-includes/comment-template.php on line 97

Notice: Trying to get property of non-object in /home/foo/public_html/wp-includes/comment-template.php on line 97

Notice: Trying to get property of non-object in /home/foo/public_html/wp-includes/comment-template.php on line 296

Notice: Trying to get property of non-object in /home/foo/public_html/wp-includes/comment-template.php on line 296

Notice: Trying to get property of non-object in /home/foo/public_html/wp-includes/comment-template.php on line 309
虽然我知道问题不在评论模板内。php文件。

我怎样才能摆脱它们?

3 个回复
SO网友:JMau

此通知是因为对象$comments 为空。所以have_comments() 在使用任何属性之前,必须包装所有内容以确保有注释。

在您的情况下:

<?php if ( have_comments() ) : 
 /* the code */
endif; ?>

SO网友:Omid Toraby

它是关于调用字段值容器的,这些字段值容器是数组,因此:

   get_comment_author()
应更改为以下内容:

   $commenter[\'comment_author\']

SO网友:clap

当我试图更改注释表单输入字段的默认位置时,我也遇到了同样的问题。出现此错误时,请首先验证并检查注释模板上的条件。同时尝试define(\'WP_DEBUG\', false);