comment_reply_link没有显示

时间:2013-08-31 10:20:16

标签: wordpress

我想创建自己的评论模板,所以我确实运行了foreach,结果出来了:

<dl class="commentlist">
    <?php foreach ($comments as $comment) : ?>
        <dt><?php printf(__('%s'), get_comment_author_link()) ?> <em><?php echo human_time_diff( get_comment_time('U'), current_time('timestamp') ); ?> <?php echo get_locale() == 'pl_PL' ? 'temu' : 'ago'; ?></em></dt>
        <dd>
            <?php if ($comment->comment_approved == '0') : ?>
                <em>Komentarz czeka na zatwierdzenie</em><br />
            <?php endif; ?>

            <?php comment_text(); ?>
            <?php comment_reply_link( array ( 'reply_text' => 'Reply this comment' ) ); ?>
        </dd>
    <?php endforeach; ?>
</dl>

一切顺利,但我无法让comment_reply_link工作。我尝试使用Wordpress Comment reply link does not appear中找到的解决方案,但它对我不起作用。

comment_reply_link( array('reply_text' => 'Reply this comment'), comment_ID(), the_ID() );

给了我一些数字(可能是评论的时间戳)。

我该怎么办?

2 个答案:

答案 0 :(得分:6)

如果您使用的不是wp_list_comments,而是使用get_comments,然后为每个运行一个,就像上面一样,comment_reply_link的问题如下:

在wordpress comment-template.php中,get_comment_reply_link使用的comment_reply_linkNULL中的值为$args['max_depth'],因此下面的if语句为:

function get_comment_reply_link($args = array(), $comment = null, $post = null) {

$defaults = array(
    'add_below'  => 'comment',
    'respond_id' => 'respond',
    'reply_text' => __('Reply'),
    'login_text' => __('Log in to Reply'),
    'depth'      => 0,
    'before'     => '',
    'after'      => ''
);

$args = wp_parse_args($args, $defaults);

if ( 0 == $args['depth'] || $args['max_depth'] <= $args['depth'] )
    return;

始终为true且函数退出过早。即使您设置了'depth' => 1。因为NULL总是小于0,1等。 您无法为comment_reply_link编写自定义过滤器,因为稍后在函数中调用该钩子。

我在没有更改comment-template.php文件的情况下找到的唯一方法是在我的comment.php中执行以下操作:

$post_id = get_the_ID();
$comment_id =get_comment_ID();

//get the setting configured in the admin panel under settings discussions "Enable threaded (nested) comments  levels deep"  
$max_depth = get_option('thread_comments_depth');
//add max_depth to the array and give it the value from above and set the depth to 1
$default = array(
    'add_below'  => 'comment',
    'respond_id' => 'respond',
    'reply_text' => __('Reply'),
    'login_text' => __('Log in to Reply'),
    'depth'      => 1,
    'before'     => '',
    'after'      => '',
    'max_depth'  => $max_depth
    );
                    comment_reply_link($default,$comment_id,$post_id); 

然后链接将显示出来。

答案 1 :(得分:1)

好的,这将是一个很长的答案:)

1.创建 commnets.php 并添加以下代码:

<?php if ( post_password_required() ): ?>   
<p class="nopassword"><?php _e( 'This post is password protected. Enter the password to view and post comments.' , 'override' ); ?></p>  

 <?php   
 return;  
 endif;  
 ?>  
    <?php if(!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME'])) : ?>
    <?php die('The comments template cannot be accessed outside of an entry. Nice try!'); ?>
    <?php endif; ?>
    <?php
    $required_text='';
    $aria_req='';
    ?>
    <?php $args = array(
'id_form' => 'commentform',
'id_submit' => 'submit',
'title_reply' => __( 'Leave a Reply' ),
'title_reply_to' => __( 'Leave a Reply to %s' ),
'cancel_reply_link' => __( 'Cancel Reply' ),
'label_submit' => __( 'Post Comment' ),
'comment_field' => '<br/><p class="comment-form-comment"><label for="comment">' . _x( 'Comment post', 'noun' ) . '</label><br/><textarea id="comment" name="comment" cols="45" rows="8"></textarea></p>',//Removed from textarea aria-required="true" HTML5 fix
'must_log_in' => '<p class="must-log-in">' .  sprintf( __( 'You must be <a href="%s">logged in</a> to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( ) ) ) ) . '</p>',
'logged_in_as' => '<p class="logged-in-as">' . sprintf( __( 'Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( ) ) ) ) . '</p>',
'comment_notes_before' => '<p class="comment-notes">' . __( 'Your email address will not be published.' ) . ( $req ? $required_text : '' ) . '</p>',
'comment_notes_after' => '<p class="form-allowed-tags">' . sprintf( __( 'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: %s' ), '<br/><code>' . allowed_tags() . '</code>' ) . '</p>',
'fields' => apply_filters( 'comment_form_default_fields', array(
'author' => '<div class="commnet_text_wrapper"><p class="comment-form-author">' . '<label for="author">' . __( 'Name', 'override' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) . '<input class="author" id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>',
'email' => '<p class="comment-form-email"><label for="email">' . __( 'Email', 'override' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) . '<input class="email" id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>',
'url' => '<p class="comment-form-url"><label for="url">' . __( 'Website', 'override' ) . '</label>' . '<input id="url" class="website" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></p></div>' ) ) );

 ?>

     <?php comment_form($args); ?>
     <ol class="commentlist">
     <?php wp_list_comments('type=comment&callback=mytheme_comment'); ?>
       </ol>
      <div class="paginate-com">
      <?php paginate_comments_links(
      array('prev_text' => __('&lsaquo; Previous comment','override'), 'next_text' => __('Next commnet &rsaquo;','override'))
      ); ?>
     </div>

将该文件上传到主题根目录(主题 index.php 所在的位置)。

2.打开 functions.php 并添加以下代码:

      // Enable CUSTOM "REPLY" COMMENT FORM so we can style it
      function mytheme_comment($comment, $args, $depth) {
    $GLOBALS['comment'] = $comment;
    extract($args, EXTR_SKIP);

    if ( 'div' == $args['style'] ) {
        $tag = 'div';
        $add_below = 'comment';
    } else {
        $tag = 'li';
        $add_below = 'div-comment';
    }
            ?>
    <<?php echo $tag ?> <?php comment_class(empty( $args['has_children'] ) ? '' : 'parent') ?> id="comment-<?php comment_ID() ?>">
    <?php if ( 'div' != $args['style'] ) : ?>
    <div id="div-comment-<?php comment_ID() ?>" class="comment-body">
    <?php endif; ?>
    <div class="comment-author vcard">
    <?php if ($args['avatar_size'] != 0) echo get_avatar( $comment, $args['avatar_size'] ); ?>
    <br/><?php printf(__('<span class="saywrap"><span class="fn">%s</span> <span class="says">says:</span></span>','override'), get_comment_author_link()) ?>
    </div>
            <?php if ($comment->comment_approved == '0') : ?>
    <em class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation.') ?></em>
    <br />
           <?php endif; ?>



    <?php comment_text() ?>
            <div class="comment-meta commentmetadata"><a class="commentlink" href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>">
        <?php
            /* translators: 1: date, 2: time */
            printf( __('%1$s at %2$s','override'), get_comment_date(),  get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)','override'),'  ','' );
        ?>
    </div>
    <div class="reply">
    <?php comment_reply_link(array_merge( $args, array('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
    </div>
    <?php if ( 'div' != $args['style'] ) : ?>
    </div>
    <?php endif; ?>
            <?php
            }

3.找到位于主题根目录中的 single.php 文件,然后调用 你的通讯表形式如下:

<div class="comments-template">     
<?php comments_template(); ?>
</div>