自定义评论Walker返回空白?

时间:2019-07-18 23:56:31

标签: php wordpress

我试图创建一个自定义的注释行,因为我想更改Wordpress输出其注释HTML结构的方式,以匹配UIKit框架的样式,可以在此处找到https://getuikit.com/docs/comment

在花了点力气让Wordpress最终看到我的自定义Comment Walker之后,我让它读取了文件,但它不输出任何注释。整个评论部分为空白,我希望你们中的一个可以告诉我为什么?

是的,显然有评论。 :)并在帖子上发表评论。如果我取消对自定义评论沃克的呼叫,评论将显示正常,因此沃克确实有用。

我从Wordpress文档中找到了一个示例,该示例被公认为“标记为删除”,但似乎应该可以使用:https://codex.wordpress.org/Function_Reference/Walker_Comment

代码似乎仍然有用,我只需要将一行更改为function start_el( &$output, $comment, $depth = 0, $args = [], $id = 0 ) {即可删除它所生成的烦人的PHP 7警告消息。 (而且只有在确认代码已经失效后,我才这样做。)

functions.php

//COMMENT
class u3_walker_comment extends Walker_Comment { //https://codex.wordpress.org/Function_Reference/Walker_Comment
    // init classwide variables
    var $tree_type = 'comment';
    var $db_fields = array( 'parent' => 'comment_parent', 'id' => 'comment_ID' );
    /** CONSTRUCTOR
     * You'll have to use this if you plan to get to the top of the comments list, as
     * start_lvl() only goes as high as 1 deep nested comments */
    function __construct() { 
        ?>
        <h3 id="comments-title">Comments</h3>
        <ul id="mycomment-list">
        <?php }
    /** START_LVL 
     * Starts the list before the CHILD elements are added. Unlike most of the walkers,
     * the start_lvl function means the start of a nested comment. It applies to the first
     * new level under the comments that are not replies. Also, it appear that, by default,
     * WordPress just echos the walk instead of passing it to &$output properly. Go figure.  */
    function start_lvl( &$output, $depth = 0, $args = array() ) {       
        $GLOBALS['comment_depth'] = $depth + 1; ?>
        <ul class="children">
        <?php }
    /** END_LVL 
     * Ends the children list of after the elements are added. */
    function end_lvl( &$output, $depth = 0, $args = array() ) {
        $GLOBALS['comment_depth'] = $depth + 1; ?>
    </ul><!-- /.children -->
<?php }
/** START_EL */
function start_el( &$output, $comment, $depth = 0, $args = [], $id = 0 ) {
    $depth++;
    $GLOBALS['comment_depth'] = $depth;
    $GLOBALS['comment'] = $comment; 
    $parent_class = ( empty( $args['has_children'] ) ? '' : 'parent' ); ?>
    <li <?php comment_class( $parent_class ); ?> id="comment-<?php comment_ID() ?>">
        <div id="comment-body-<?php comment_ID() ?>" class="comment-body">
            <div class="comment-author vcard author">
                <?php echo ( $args['avatar_size'] != 0 ? get_avatar( $comment, $args['avatar_size'] ) :'' ); ?>
                <cite class="fn n author-name"><?php echo get_comment_author_link(); ?></cite>
            </div><!-- /.comment-author -->
            <div id="comment-content-<?php comment_ID(); ?>" class="comment-content">
                <?php if( !$comment->comment_approved ) : ?>
                    <em class="comment-awaiting-moderation">Your comment is awaiting moderation.</em>
                    <?php else: comment_text(); ?>
                    <?php endif; ?>
                </div><!-- /.comment-content -->
                <div class="comment-meta comment-meta-data">
                    <a href="<?php echo htmlspecialchars( get_comment_link( get_comment_ID() ) ) ?>"><?php comment_date(); ?> at <?php comment_time(); ?></a> <?php edit_comment_link( '(Edit)' ); ?>
                </div><!-- /.comment-meta -->
                <div class="reply">
                    <?php $reply_args = array(
                        'add_below' => $add_below, 
                        'depth' => $depth,
                        'max_depth' => $args['max_depth'] );
                        comment_reply_link( array_merge( $args, $reply_args ) );  ?>
                    </div><!-- /.reply -->
                </div><!-- /.comment-body -->
            <?php }
            function end_el(&$output, $comment, $depth = 0, $args = array() ) { ?>
            </li><!-- /#comment-' . get_comment_ID() . ' -->
        <?php }
    /** DESTRUCTOR
     * I just using this since we needed to use the constructor to reach the top 
     * of the comments list, just seems to balance out :) */
    function __destruct() { ?>
    </ul><!-- /#comment-list -->
<?php }
}
//COMMENT

comments.php

<?php new u3_Walker_Comment() ; ?>

post.php

            <?php
            if ( comments_open() || get_comments_number() ) {
                comments_template('/html/comments.php', true);
            }; 
            ?>

页面上的结果仅为<h3 id="comments-title">Comments</h3><ul id="mycomment-list"> </ul>。评论列表完全空白!

0 个答案:

没有答案