Make WordPress Core

Opened 15 months ago

Closed 15 months ago

Last modified 11 months ago

#59422 closed defect (bug) (worksforme)

Custom Post Type name with hyphen does not with custom single post template

Reported by: htung's profile htung Owned by:
Milestone: Priority: normal
Severity: normal Version: 6.3.1
Component: Themes Keywords:
Focuses: template Cc:

Description

Hi,

I have registered a new custom post type namely "annual-report". Then I try to create singe-annual-report.php in theme folder to call out single post detail page, however it seems not working.

While single-$postname.php works perfectly when it comes to custom post type name with single word without hypen, such as "annualreport".

May I know is there is any restriction of WP custom post type name and applied on theme file calling?

Thank you.

Attachments (1)

Screenshot 2023-09-22 at 4.16.26 PM.png (340.8 KB) - added by faisal03 15 months ago.

Download all attachments as: .zip

Change History (6)

#1 @faisal03
15 months ago

@htung Thanks for the report.

Using a different character, such as a normal hyphen "-" or a longer one "—" could potentially lead to issues. Can you please double-check and make sure you are using the normal hyphen?

#2 @htung
15 months ago

@faisal03 As checking on the code, it is normal hyphen. The "annual-report" CPT is working normally at other functionalities, such as add/editing/listing post but when comes to calling in single post theme file is not linked.

Code below is part of post type registration:

<?php
$postTypeName = normalize_whitespace(strtolower(preg_replace("/[^A-Za-z0-9 ]/", '-', 'annual-report')));
$postTypeNames[$postTypeName] = $postType->post_title;      
$entMetabox->postType = $postTypeName;

$args =  array(
        'labels' => array(
                'name' => __($postType->post_title),
                'singular_name'      => __( $postType->post_title), 
                'menu_name'          => __( $postType->post_title . 's'),
                'name_admin_bar'     => __( $postType->post_title . 's'),
                'add_new'            => __( 'Add New'),
                'add_new_item'       => __( 'Add New ' . $postType->post_title),
                'edit_item'          => __( 'Edit ' . $postType->post_title),
                'new_item'           => __( 'New Example'),
                'view_item'          => __( 'View ' . $postType->post_title),
                'search_items'       => __( 'Search '.$postType->post_title.'s'),
                'not_found'          => __( 'No '.$postType->post_title.'s found'),
                'not_found_in_trash' => __( 'No '.$postType->post_title.'s found in trash'),
                'all_items'          => __( 'All '.$postType->post_title.'s'),

                /* Labels for hierarchical post types only. */
                'parent_item'        => __( 'Parent '.$postType->post_title),
                'parent_item_colon'  => __( 'Parent '.$postType->post_title.':'),

                /* Custom archive label.  Must filter 'post_type_archive_title' to use. */
                'archive_title'      => __( $postType->post_title . 's'),
        ),
        'supports' => isset($postType->{'supports'}) 
                ? $postType->{'supports'}
                : array(
                        'title', 
                        'editor', 
                        // 'excerpt', 
                        // 'custom-fields', 
                        // 'page-attributes',
                        'thumbnail',
                        ),
        'public' => isset($postType->{'public'}) ? $postType->{'public'} : true,
        'show_in_rest' => isset($postType->{'show_in_rest'}) ? $postType->{'show_in_rest'} : false, /* Enable to have Gutenberg editor. */
        'show_in_menu' => $parentPath,
        'taxonomies' => isset($postType->{'taxonomies'}) ? array_map('trim',explode(',',$postType->{'taxonomies'})) : array('post_tag','category'),
        'show_ui' => isset($postType->{'show_ui'}) ? $postType->{'show_ui'} : true,
        'capability_type'     => array( $postTypeName, $postTypeName.'s'), /** Add controller for user roles */
        'map_meta_cap'        => isset($postType->{'map_meta_cap'}) ? $postType->{'map_meta_cap'} : true, /** Add controller for user roles */
        'menu_position' =>  10,
        'menu_icon' => isset($postType->{'menu_icon'}) ? $postType->{'menu_icon'} : 'dashicons-format-aside'
);

register_post_type( $postTypeName, $args);

#3 @faisal03
15 months ago

@htung thanks for the details. I have used your code and the CPT post displayed for me using single-annual-report.php. See the screenshot above.

Please make sure that you are passing true in public.

'public' => isset($postType->{'public'}) ? $postType->{'public'} : true,

If this is false, you will not see the post detail page even if you use single-annual-report.php.

#4 @htung
15 months ago

  • Resolution set to worksforme
  • Status changed from new to closed

@faisal03 Thanks for testing it for me, actually I found the issue was due to missing below parameter. Once I declared it, the single-annual-report.php is working.

'rewrite' => array('slug' => 'annual-report' ),

#5 @swissspidy
11 months ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.