1

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.

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?

  • Where is comments_template() (the function that actually includes comments.php in the template) being called? Is it being called by the_field( 'comments_section' )? – Chip Bennett Nov 24 '13 at 16:39
  • comments_template() is called inside single-CPT.php. I've updated my question with the code which is calling it – agis Nov 24 '13 at 16:48
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 );
}
  • I've tried this but is not working properly. After I add this code, inside my single post I see 2 comment forms and if I enter on my mywebsite/single-post/comments I see just one.Your code seems to work but It's adding an extra comment form inisde my main post. I've updated my question with how I've registered that array key. – agis Nov 24 '13 at 15:44
  • You need to update your question with your actual template code that outputs the comment form. Otherwise, specific help is impossible. – Chip Bennett Nov 24 '13 at 16:03
  • I've updated my question with the code from my comments.php – agis Nov 24 '13 at 16:11
  • Your comments.php markup does not include the array_key_exists() conditional. Where have you tried to place it? – Chip Bennett Nov 24 '13 at 16:17
  • 1
    I inadvertently left of the ! in the array_key_exists() conditional. – Chip Bennett Nov 24 '13 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.