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
- Renaming Custom Post Types and Taxonomies
- Mixing custom post type and taxonomy rewrite structures?
- Resolve a custom post type name vs. page permalink conflict (same slug)
- Mixing regular and custom post types (with meta_query) on home page
- Inconsistent temporary 404s on whole install | PHP error with post-template.php
- Display current taxonomy term when inside custom post type
- Adding custom post type to loop
- How can I add dropdown widget/box to admin post page?
- Get posts with tag all post types
- Debug output during filter execution doesn’t work
- No results searching custom post type in admin panel
- handling csv data with a custom post type
- Mapping Subdomains in WordPress to give the appearance of a localised site, best approach?
- What is the recommended / best way to do this: Event calendar post/type in a block-based theme?
- Polylang non-default language ignores tags in WP_Query
- Function causing the loop display posting more (empty)
- List of Posts and Categories
- Custom fields for custom post type
- Show only public custom posts in home page
- Custom Post Type Link added to menu won’t show in frontend
- Taxonomy archive 404ing (not term archive)
- Alphabetically sort a taxonomy.php template by post title
- Show Taxonomies with admin area for custom post type?
- Loading plugin text domain before registering post type
- Menu’s breaking, now showing all page links on site
- Custom PT, Taxonomys and wordpress query
- new post-type how do i retain the plugins on my sidebar?
- How to hide list from custom post type
- Custom Post Type Dashboard Tab not displaying any posts
- How to display grandchildren only of custom post?
- Display Custom Post Type Archive content with Shortcode
- List all posts associated under custom taxonomy
- Admin metabox with custom post type dropdown option not update choosed input after saving
- Making a archive gallery page for a custom post type which already has an archive page
- Cant edit custom post type in WordPress dashboard
- Custom Post Type parent slug as menu item
- Advanced Custom Fields – Add Field to Specific Page [closed]
- How to internationalize the sub menu name “categories” in Custom post type?
- Omitted Content After Post-Publish Insertion
- WordPress custom post type odd and even styling [duplicate]
- Add categories to custom post type