strange conditional tag behaviour [duplicate]

In the first version of your code, you don’t check if user is logged in.

In the second attempt your logic is wrong, I guess.

So your code should look like so:

<?php
    // If CPT and not logged in, display a message:
    if ( 'CPT' == 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 ( 'CPT' != get_post_type() || (is_user_logged_in() && array_key_exists( 'comments', $wp_query->query_vars )) ) {
        comment_form();
    }
?>

This code will:

  • Show message “You must be …” if user is not logged in and is viewing CPT.
  • Show no such message on other post types.
  • Show comment form on CPT only if user is logged in.
  • Show comment form on other post types always.