Make WordPress Core

#57422 closed defect (bug) (duplicate)

get_body_class() adding class "{$post_type}-template-{dir}{file}"

Reported by: arrancurran's profile arrancurran Owned by:
Milestone: Priority: normal
Severity: minor Version: 6.1.1
Component: Posts, Post Types Keywords:
Focuses: css, template Cc:

Description (last modified by sabernhardt)

When is_singular() and is_page_template() and the template file is inside a directory within the theme folder, get_body_class() adds a class with a missing hyphen between the template directory and the template file name.

post-template.php line 677;

$classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( '.', '-', $template_slug ) );

Example: post_type is page and template file is, page-templates/my-template-file.php

Result: .page-template-page-templatesmy-template-file-php

Suggested change to post-template.php line 677;

$classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( array( '.', '/' ), '-', $template_slug ) );

Change History (3)

#1 @sabernhardt
19 months ago

  • Component changed from General to Posts, Post Types
  • Description modified (diff)
  • Keywords changes-requested removed

#2 @SergeyBiryukov
19 months ago

  • Keywords close added

Hi there, welcome to WordPress Trac! Thanks for the report.

Replying to arrancurran:

Example: post_type is page and template file is, page-templates/my-template-file.php

Result: .page-template-page-templatesmy-template-file-php

This was previously discussed in #23470 and improved in [30100] in a way that is similar to the suggestion here.

With the example above, a class like this should also be added:

.page-template-page-templates-my-template-file

However, the consensus was that the old class needs to remain for backward compatibility and cannot be removed.

#3 @SergeyBiryukov
19 months ago

  • Keywords close removed
  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

This appears to be a duplicate of #41518, let's continue the discussion there.

Note: See TracTickets for help on using tickets.