disable comments if array exists

This is the code which is registering the function for 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
I don’t know if this matters but I will write it.

Read More

I have single-CPT.php which is calling /inc/post-format/single-CPT.php like this:

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

<?php 

get_template_part( 'inc/post-format/single-debate'); 
    ...

This is the code which is checking if array key exists, which is inside my /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
 }
  ?>

This is my 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 } ?> 

This is how the comments template is included inside my single-CPT.php :

if ( comments_open()  )

comments_template( '', true );

I want to disable comments if if( array_key_exists( 'comments', $wp_query->query_vars ) ){

How can I do this?

Related posts

1 comment

  1. Assuming you’re properly using comment_form() to output the comment reply form:

    if( ! array_key_exists( 'comments', $wp_query->query_vars ) ) {
        // Output the comment form
        comment_form();
    }
    

    Edit

    Based on your comments.php code:

    // 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();
        }
    }
    

    I’m taking a complete stab in the dark here, because it’s not entirely clear how your comments template is being included in your template.

    Edit 2

    I don’t think this will work as intended:

    if ( comments_open()  )
    
    comments_template( '', true );
    

    See that extra line break? Without curly braces around your conditional, the two lines aren’t related. You need to do one of the following:

    if ( comments_open() )
        comments_template( '', true );
    

    or, better yet:

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

Comments are closed.