首先,这是我想要实现的目标:
我想在CPT上添加一个评论挂钩,它要求用户注册并登录,以便能够发布评论。用户登录后,在注释字段之前显示两个自定义字段。
我的讨论设置未设置为*用户必须注册并登录到评论*,因为在我的默认博客帖子中,我不想强制用户注册才能发表评论。
以下是我到目前为止所做的:
function debate_comment_fields( $fields ) {
 if( is_singular( \'debate\' ) ) {
  // these are the custom fields which I want to display before the comment field 
    $fields[\'first\'] = \'<p class="comment-form-first"><label for="first">\' . 
                     __( \'HTML5\' ) . 
                     \'</label>\' . 
                     \'<input id="first" name="category" type="radio" value="\'.get_post_meta($post->ID, \'agree\', true).\'" /></p>\';
    $fields[\'second\'] = \'<p class="comment-form-second"><label for="second">\' . 
                       __( \'FLASH\' ) . 
                       \'</label>\' .
                      \'<input id="second" name="category" type="radio" value="\'.get_post_meta($post->ID, \'disagree\', true).\'" /></p>\';
    return $fields;
}
else {
         return $fields;
}
} 
add_filter(\'comment_form_default_fields\',\'debate_comment_fields\');  
 我翻遍了抄本,发现了这个
comment_form_must_log_in_after  但这对我没有帮助,因为我首先需要添加一个钩子,它要求用户登录。
我还研究了神话/评论。php以下是一个片段:
  <div id="comments" class="comments-area">
    <?php if ( have_comments() ) : ?>
        <h3 class="comments-title">
            <?php
                printf( _n(\'%d comment\', \'%d comments\', get_comments_number(), \'outbox\' ),
                    number_format_i18n( get_comments_number() ) );
            ?>
        </h3>
        <ol class="commentlist">
            <?php
                wp_list_comments( array( \'callback\' => \'outbox_comment\' ) );
            ?>
        </ol><!-- .commentlist -->
        <?php if ( get_comment_pages_count() > 1 && get_option( \'page_comments\' ) ) : // are there comments to navigate through? If so, show navigation ?>
        <nav role="navigation" id="comment-nav-below" class="site-navigation comment-navigation clearfix">
            <div class="nav-previous"><i class="icon-left-open-1"></i> <?php echo get_previous_comments_link( __( \'Older Comments\', \'outbox\' ) ); ?></div>
            <div class="nav-next"><?php echo get_next_comments_link( __( \'Newer Comments\', \'outbox\' ) ); ?> <i class="icon-right-open-1"></i></div>
        </nav><!-- #comment-nav-below .site-navigation .comment-navigation -->
        <?php endif; ?>
    <?php endif; // have_comments() ?>
    <?php
        // If comments are closed and there are comments, let\'s leave a little note, shall we?
        if ( ! comments_open() && \'0\' != get_comments_number() && post_type_supports( get_post_type(), \'comments\' ) ) :
    ?>
        <p class="nocomments"><?php _e( \'Comments are closed.\', \'outbox\' ); ?></p>
    <?php endif; ?>
    <?php comment_form(); ?>
 这是“必须登录”:
        \'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( $post_id ) ) ) ) . \'</p>\',
 你知道我该如何做到这一点吗?
非常感谢。
 
                    最合适的回答,由SO网友:Chip Bennett 整理而成
                    另一种选择是,如果当前用户未登录,使用is_user_logged_in().
例如,在注释模板中:
<?php comment_form( $args ); ?>
 只需将其包装在条件中:
<?php
// Don\'t output the comment form if CPT and user isn\'t logged in
if ( \'debate\' != get_post_type() || is_user_logged_in() ) {
    comment_form( $args );
}
?>
 编辑只需将其直接放入模板中:
<?php
// If CPT and not logged in, display a message:
if ( \'debate\' == get_post_type() && ! is_user_logged_in() ) {
    echo \'<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( $post_id ) ) ) ) . \'</p>\';
}
?>