如果数组存在,则禁用注释

时间:2013-11-24 作者:agis

这是为注册函数的代码rewrite_endpoints():

function wpa121567_rewrite_endpoints(){

add_rewrite_endpoint( \'comments\', EP_PERMALINK );

add_rewrite_endpoint( \'stats\', EP_PERMALINK );

}

add_action( \'init\', \'wpa121567_rewrite_endpoints\' );
NOTE 我不知道这是否重要,但我会写下来的。

我有single-CPT.php 正在呼叫/inc/post-format/single-CPT.php 像这样:

    <?php while ( have_posts() ) : the_post(); ?>

<?php 

get_template_part( \'inc/post-format/single-debate\'); 
    ...
这是正在检查array key exists, 在我的/inc/post-format/single-cpt.php:

?php
if( array_key_exists( \'comments\', $wp_query->query_vars ) ){
 the_field(\'comments_section\'); 

} elseif( array_key_exists( \'stats\', $wp_query->query_vars ) ) {

 the_field(\'stats_section\'); 
 } else {
// the request is for the main post
 }
  ?>
这是我的comments.php :

     <div id="comments" class="comments-area">
<?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" style="padding-left:20px; font-size:20px;">You must be logged in to post a comment.\' . \'</p>\';

echo do_shortcode("[upme_login]");

 }
?>
    <?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>&nbsp;<?php echo get_previous_comments_link( __( \'Older Comments\', \'outbox\' ) ); ?></div>
            <div class="nav-next"><?php echo get_next_comments_link( __( \'Newer Comments\', \'outbox\' ) ); ?>&nbsp;<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
 // 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();
}

?>

</div><!-- #comments .comments-area -->
<?php } ?> 
这就是注释模板如何包含在我的single-CPT.php :

if ( comments_open()  )

comments_template( \'\', true );
我想禁用注释,如果if( array_key_exists( \'comments\', $wp_query->query_vars ) ){

我该怎么做?

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

假设您正确使用comment_form() 要输出评论回复表单,请执行以下操作:

if( ! array_key_exists( \'comments\', $wp_query->query_vars ) ) {
    // Output the comment form
    comment_form();
}
根据您的comments.php 代码:

// If the user isn\'t logged in, don\'t display comment form
if ( is_user_logged_in() ) {
    global $wp_query;
    // If the post type isn\'t \'debate\',
    // Or if the \'comments\' array key exists in the query
    // Display the comment form
    if ( \'debate\' != get_post_type() || ! array_key_exists( \'comments\', $wp_query->query_vars ) ) {
        comment_form();
    }
}
我在这里完全是瞎猜,因为还不完全清楚你的评论模板是如何包含在模板中的。

编辑2

我认为这不会像预期的那样起作用:

if ( comments_open()  )

comments_template( \'\', true );
看到那个额外的换行符了吗?如果条件周围没有大括号,这两行就没有关系。您需要执行以下操作之一:

if ( comments_open() )
    comments_template( \'\', true );
或者,更好的是:

if ( comments_open() ) {
    comments_template( \'\', true );
}

结束

相关推荐

Disallow img tag in comments?

我一直在寻找一种方法来阻止人们在评论中发布外部图像,我<img alt=\"my photo\" src=\"http://example.co.uk/pic.jpg\" /> 并显示图像,无需所有者的许可。这在核心的某个地方是硬编码的,但我找不到一种方法来消除这种行为。谢谢