1

我想创建自己的评论模板,所以我确实运行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( array('reply_text' => 'Reply this comment'), comment_ID(), the_ID() );

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

我能做些什么?

4

2 回答 2

7

例如,如果您使用 not using wp_list_commentsbutget_comments然后为每个运行 a,就像您在上面所做的那样,问题comment_reply_link如下:

在 wordpress 的 comment-template.php 中,get_comment_reply_link使用的 by在下面的 if 语句中comment_reply_link有一个NULL值:$args['max_depth']

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;

始终为真,并且函数过早退出。即使你设置'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); 

然后链接就会出现。

于 2013-12-30T00:43:56.617 回答
2

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

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文件,然后像这样调用您的 commnet 表单:

<div class="comments-template">     
<?php comments_template(); ?>
</div>
于 2013-08-31T21:16:01.583 回答