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.
Related Posts:
- changing default comment form arguments [duplicate]
- How do test if a post is a custom post type?
- Conditional for single-{post-type}.php
- is_singular() not working if called via callback function of admin-ajax.php
- functions.php conditional tag only for custom post type
- Exclude custom function content from certain pages
- Get template part based on custom taxonomy term
- Add comments meta fields to comments metabox on post edit screen
- If on term-page -> get the current term?
- custom post type upcoming post and past post
- is_page_template not working as expected
- remove child post from custom post type archive
- How to add extra input fields to CPT’s comment form?
- if custom posts type exists and there are posts load script
- Need help targeting a custom post type with conditional tags
- Why would this IF statement not work? [closed]
- custom comments on specific post type
- Excluding custom post types
- Register widget only page is a singular of custom post type
- Is there a conditional tag to determine whether the post is _any_ custom post type?
- Conditional Query of Custom Post Type and custom taxonomy
- How to run filter only on custom post type edit screen
- comment_form() not showing on custom post type
- Conditional statement for if archive page has posts which contain certain taxonomies/categories/tags, show those terms
- Custom Post Conditional for Parent & Child Single.php?
- What functions does WordPress use for filtering / sanitizing comments?
- Conditional for a Single Post That Belongs to a Category?
- How to show only the most recent post on my custom post type archive?
- Conditional Tags for specific category of custom posts
- Move Genesis Single Page/Single Post Title
- How to check if post/page or taxonomy post is published by admin
- Adding guestbook to my wordpress site
- TinyMCE Buttons on Certain Post Type
- Display message if no posts in Custom Post Type loop
- Showing taxonomy terms on custom post type
- Comment form on custom page template
- duplicate comment section functionality and call it something else for custom post types
- Check if custom post is a parent?
- Send email messages after comment was submitted
- Redirect to URL if x number of days passed
- comments hooks on custom post type
- Conditional Statements
- If custom taxonomy else conditional [closed]
- If tag exists, then echo once
- Show specific content on parent custom post type and all children
- Use conditionals with wp_enqueue_style to attach stylesheet according to post type displayed
- How can I check the rewrite slug of current post type listing page
- How to add custom PHP code in post header?
- $_SESSION Not working after X amount of page views
- conditional based upon the count of current user’s published custom posts
- Custom metabox fields not saving when limited to a certain CPT
- Can not hook into custom post type template with: is_page() conditional?
- Comments not enabled for custom post types
- Diffrent search templates for different post types
- Conditional Tags If Custom Post Parent & Child?
- Custom Post Query Combined with Conditional Tags
- Conditional statement checking two conditions for CPT and Custom Taxonomy [closed]
- Error 403 when posting comments to a custom post type from a different page
- Custom post type is_singular condtional not working when managing sidebar display
- Add tags to custom post type without menu link
- Display custom taxonomy on my custom post type
- List Categories (wp_terms) and count posts
- Post loop count is not in order
- Gravity forms how to display list of submitters on the post submitted page [closed]
- Add Previous/Next Buttons to CPT Single Pages Only
- Problem with $post_id object’s property
- get_post_types is not showing all registered posts
- How To Display A List Of CPT Posts In A Meta Box On Menus Page?
- Control content before and after custom post type loop
- Custom Post Types Archives and Single Pages not showing custom taxonomy data
- Custom post doesn’t show featured image
- Showing custom post type categories in the menu
- Displaying custom post types by category
- How to setup a single post page for my custom post type?
- Disable custom taxonomy on admin bar
- Conflict between Capabilities and Menu Visibility with Custom Post Types
- Custom Post not working as expected
- How to use a variable as a function name?
- What do the numbers mean at the end of add_action(‘save_post’)…?
- How to use next_posts_link/previous_posts_link for custom post types paginate?
- Getting WordPress Flexslider Item Number
- Designing a custom post type with a minimum number of meta fields
- Why does my taxonomy have a category style div id?
- Sorting posts by custom field value in array
- Custom post type paging 404 error
- Targeting categories in custom fields
- Display Only Certain Category In a Custom Page Template
- Saving Meta Data within Custom Post Type
- A meta box (in a custom post type) with two different type of fields
- Redirect to first child on Custom Post Type (without template)
- Assign CPT archive page attributes
- Move content between custom post types
- Custom post types problem
- How to get postname in custom page?
- Query custom post type based on post id and custom field value
- Pods CMS: How to add custom column to Adnvanced Content Type
- Custom post type, custom taxonomy, query posts only from taxonomy (children of)
- Creating Sections for Post Types
- Give each post type label a different color?
- page not found for single-type.php file