注释回调的问题(参数#1不是数组?)

时间:2018-07-15 作者:J. Amy

我使用class-walker-comment.php 作为参考,我不知道为什么它不起作用。非常感谢您的帮助。

我遇到的问题是:

1) 化身不显示。

2) 此错误显示在每个注释上方:

警告:array\\u merge()[函数.数组合并]:参数#1不是数组

该错误似乎是我的函数中此位代码的结果:

comment_reply_link(array_merge
下面是我使用的完整回调函数:

function custom_comment( $comment, $depth, $args ) { 

$tag = ( \'div\' === $args[\'style\'] ) ? \'div\' : \'li\'; ?>

<<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $args[\'has_children\'] ? \'parent\' : \'\', $comment ); ?>>

    <article id="div-comment-<?php comment_ID(); ?>" class="comment-inner">
    <div class="comment-header"> 

    <?php  
    if ( 0 != $args[\'avatar_size\'] ) { ?>

        <div class="comment-avatar">
            <?php echo get_avatar( $comment, $args[\'avatar_size\'] ); ?>
        </div>

    <?php } ?>

        <div class="comment-meta-wrap">
        <div class="comment-author vcard">
            <!--  translators: %s: comment author link -->
            <?php printf(   __( \'%s <span class="says">says:</span>\' ), sprintf( \'<b class="fn">%s</b>\', get_comment_author_link( $comment ) ) ); ?>
        </div><!-- .comment-author -->

        <div class="comment-meta commentmetadata">

        <a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
        <time datetime="<?php comment_time( \'c\' ); ?>">
        <!-- translators: 1: comment date, 2: comment time -->
            <?php printf( __( \'%1$s at %2$s\' ), get_comment_date( \'\', $comment ), get_comment_time() ); ?>
        </time>
        </a>

        <?php edit_comment_link( __( \'Edit\' ), \'<span class="edit-link">\', \'</span>\' ); ?>

        </div><!-- .comment-meta -->

        </div><!-- .comment-meta-wrap -->

        </div><!-- .comment-header --> <div class="clearboth"></div>

        <div class="comment-content">

        <?php if ( \'0\' == $comment->comment_approved ) : ?>
            <p class="comment-awaiting-moderation"><?php _e( \'Your comment is awaiting moderation.\' ); ?></p>
        <?php endif; ?>

            <?php comment_text(); ?>

        </div><!-- .comment-content -->

        <?php
        comment_reply_link(
            array_merge(
                $args, array(
                    \'add_below\' => \'div-comment\',
                    \'depth\'     => $depth,
                    \'max_depth\' => $args[\'max_depth\'],
                    \'before\'    => \'<div class="reply">\',
                    \'after\'     => \'</div>\',
                )
            )
        ); ?>

        </article><!-- .comment-inner -->
下面是我的评论。php:

<?php
// You can start editing here -- including this comment!
if ( have_comments() ) :
?>
    <h2 class="comments-title">
        <?php
        $comments_number = get_comments_number();
        if ( \'1\' === $comments_number ) {
            /* translators: %s: post title */
            printf( _x( \'One Reply to &ldquo;%s&rdquo;\', \'comments title\', \'textdomain\' ), get_the_title() );
        } else {
            printf(
                /* translators: 1: number of comments, 2: post title */
                _nx(
                    \'%1$s Reply to &ldquo;%2$s&rdquo;\',
                    \'%1$s Replies to &ldquo;%2$s&rdquo;\',
                    $comments_number,
                    \'comments title\',
                    \'textdomain\'
                ),
                number_format_i18n( $comments_number ),
                get_the_title()
            );
        }
        ?>
    </h2>

    <ol class="comment-list">
        <?php
            wp_list_comments(
                array(
                    \'avatar_size\' => 100,
                    \'style\'       => \'ol\',
                    \'callback\'    => \'custom_comment\',
                    \'short_ping\'  => true,
                )
            );
        ?>
    </ol>

    <?php
    the_comments_pagination(
        array(
            \'prev_text\' => \'<span class="screen-reader-text">\' . __( \'Previous\', \'textdomain\' ) . \'</span>\',
            \'next_text\' => \'<span class="screen-reader-text">\' . __( \'Next\', \'textdomain\' ) . \'</span>\',
        )
    );

endif; // Check for have_comments().

// If comments are closed and there are comments, let\'s leave a little note, shall we?
if ( ! comments_open() && get_comments_number() && post_type_supports( get_post_type(), \'comments\' ) ) :
?>

    <p class="no-comments"><?php _e( \'Comments are closed.\', \'textdomain\' ); ?></p>
<?php
endif;

comment_form();
?>

1 个回复
最合适的回答,由SO网友:BinaryMoon 整理而成

是否尝试在array\\u merge&;上进行var\\u dump;头像参数?这将告诉您这些变量是否是您所期望的。

无论如何,看起来您的参数顺序有误。

您正在使用

custom_comment( $comment, $depth, $args )
但它应该是

custom_comment( $comment, $args, $depth )
希望有帮助

结束

相关推荐

Delete comments function

我做了一个功能,向登录用户显示她的所有评论,并允许她通过与打印的每个评论相关的删除按钮单独删除这些评论。该功能起作用,但在执行表单操作后,刷新时页面仍会显示已删除的注释,并且仅当您再次转到该页面(不是刷新,而是通过单击浏览器地址栏上的返回键)时,页面才会显示已批准的注释。如何解决此问题?代码如下: function custom_delete_post_comment() { $comment_id = comment_ID();