mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 20:30:23 +00:00
Posts, Post Types: Add support for post type templates.
WordPress has supported custom page templates for over 12 years, allowing developers to create various layouts for specific pages. While this feature is very helpful, it has always been limited to the 'page' post type and not was not available to other post types. By opening up the page template functionality to all post types, we continue to improve the template hierarchy's flexibility. In addition to the `Template Name` file header, the post types supported by a template can be specified using `Template Post Type: post, foo, bar`. When at least one template exists for a post type, the 'Post Attributes' meta box will be displayed in the back end, without the need to add post type support for `'page-attributes'`. 'Post Attributes' can be customized per post type using the `'attributes'` label when registering a post type. Props johnbillion, Mte90, dipesh.kakadiya, swissspidy. Fixes #18375. git-svn-id: https://develop.svn.wordpress.org/trunk@38951 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -594,23 +594,40 @@ function get_body_class( $class = '' ) {
|
||||
if ( is_404() )
|
||||
$classes[] = 'error404';
|
||||
|
||||
if ( is_single() ) {
|
||||
if ( is_singular() ) {
|
||||
$post_id = $wp_query->get_queried_object_id();
|
||||
$post = $wp_query->get_queried_object();
|
||||
$post_type = $post->post_type;
|
||||
|
||||
$classes[] = 'single';
|
||||
if ( isset( $post->post_type ) ) {
|
||||
$classes[] = 'single-' . sanitize_html_class($post->post_type, $post_id);
|
||||
$classes[] = 'postid-' . $post_id;
|
||||
if ( is_page_template() ) {
|
||||
$classes[] = "{$post_type}-template";
|
||||
|
||||
// Post Format
|
||||
if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
|
||||
$post_format = get_post_format( $post->ID );
|
||||
$template_slug = get_page_template_slug( $post_id );
|
||||
$template_parts = explode( '/', $template_slug );
|
||||
|
||||
if ( $post_format && !is_wp_error($post_format) )
|
||||
$classes[] = 'single-format-' . sanitize_html_class( $post_format );
|
||||
else
|
||||
$classes[] = 'single-format-standard';
|
||||
foreach ( $template_parts as $part ) {
|
||||
$classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( array( '.', '/' ), '-', basename( $part, '.php' ) ) );
|
||||
}
|
||||
$classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( '.', '-', $template_slug ) );
|
||||
} else {
|
||||
$classes[] = "{$post_type}-template-default";
|
||||
}
|
||||
|
||||
if ( is_single() ) {
|
||||
$classes[] = 'single';
|
||||
if ( isset( $post->post_type ) ) {
|
||||
$classes[] = 'single-' . sanitize_html_class( $post->post_type, $post_id );
|
||||
$classes[] = 'postid-' . $post_id;
|
||||
|
||||
// Post Format
|
||||
if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
|
||||
$post_format = get_post_format( $post->ID );
|
||||
|
||||
if ( $post_format && !is_wp_error($post_format) )
|
||||
$classes[] = 'single-format-' . sanitize_html_class( $post_format );
|
||||
else
|
||||
$classes[] = 'single-format-standard';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -619,6 +636,23 @@ function get_body_class( $class = '' ) {
|
||||
$mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' );
|
||||
$classes[] = 'attachmentid-' . $post_id;
|
||||
$classes[] = 'attachment-' . str_replace( $mime_prefix, '', $mime_type );
|
||||
} elseif ( is_page() ) {
|
||||
$classes[] = 'page';
|
||||
|
||||
$page_id = $wp_query->get_queried_object_id();
|
||||
|
||||
$post = get_post($page_id);
|
||||
|
||||
$classes[] = 'page-id-' . $page_id;
|
||||
|
||||
if ( get_pages( array( 'parent' => $page_id, 'number' => 1 ) ) ) {
|
||||
$classes[] = 'page-parent';
|
||||
}
|
||||
|
||||
if ( $post->post_parent ) {
|
||||
$classes[] = 'page-child';
|
||||
$classes[] = 'parent-pageid-' . $post->post_parent;
|
||||
}
|
||||
}
|
||||
} elseif ( is_archive() ) {
|
||||
if ( is_post_type_archive() ) {
|
||||
@@ -671,36 +705,6 @@ function get_body_class( $class = '' ) {
|
||||
$classes[] = 'term-' . $term->term_id;
|
||||
}
|
||||
}
|
||||
} elseif ( is_page() ) {
|
||||
$classes[] = 'page';
|
||||
|
||||
$page_id = $wp_query->get_queried_object_id();
|
||||
|
||||
$post = get_post($page_id);
|
||||
|
||||
$classes[] = 'page-id-' . $page_id;
|
||||
|
||||
if ( get_pages( array( 'parent' => $page_id, 'number' => 1 ) ) ) {
|
||||
$classes[] = 'page-parent';
|
||||
}
|
||||
|
||||
if ( $post->post_parent ) {
|
||||
$classes[] = 'page-child';
|
||||
$classes[] = 'parent-pageid-' . $post->post_parent;
|
||||
}
|
||||
if ( is_page_template() ) {
|
||||
$classes[] = 'page-template';
|
||||
|
||||
$template_slug = get_page_template_slug( $page_id );
|
||||
$template_parts = explode( '/', $template_slug );
|
||||
|
||||
foreach ( $template_parts as $part ) {
|
||||
$classes[] = 'page-template-' . sanitize_html_class( str_replace( array( '.', '/' ), '-', basename( $part, '.php' ) ) );
|
||||
}
|
||||
$classes[] = 'page-template-' . sanitize_html_class( str_replace( '.', '-', $template_slug ) );
|
||||
} else {
|
||||
$classes[] = 'page-template-default';
|
||||
}
|
||||
}
|
||||
|
||||
if ( is_user_logged_in() )
|
||||
@@ -1621,14 +1625,12 @@ function get_the_password_form( $post = 0 ) {
|
||||
*
|
||||
* @since 2.5.0
|
||||
* @since 4.2.0 The `$template` parameter was changed to also accept an array of page templates.
|
||||
* @since 4.7.0 Now works with any post type, not just pages.
|
||||
*
|
||||
* @param string|array $template The specific template name or array of templates to match.
|
||||
* @return bool True on success, false on failure.
|
||||
*/
|
||||
function is_page_template( $template = '' ) {
|
||||
if ( ! is_page() )
|
||||
return false;
|
||||
|
||||
$page_template = get_page_template_slug( get_queried_object_id() );
|
||||
|
||||
if ( empty( $template ) )
|
||||
@@ -1649,21 +1651,28 @@ function is_page_template( $template = '' ) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the specific template name for a page.
|
||||
* Get the specific template name for a given post.
|
||||
*
|
||||
* @since 3.4.0
|
||||
* @since 4.7.0 Now works with any post type, not just pages.
|
||||
*
|
||||
* @param int $post_id Optional. The page ID to check. Defaults to the current post, when used in the loop.
|
||||
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
|
||||
* @return string|false Page template filename. Returns an empty string when the default page template
|
||||
* is in use. Returns false if the post is not a page.
|
||||
* is in use. Returns false if the post does not exist.
|
||||
*/
|
||||
function get_page_template_slug( $post_id = null ) {
|
||||
$post = get_post( $post_id );
|
||||
if ( ! $post || 'page' != $post->post_type )
|
||||
function get_page_template_slug( $post = null ) {
|
||||
$post = get_post( $post );
|
||||
|
||||
if ( ! $post ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$template = get_post_meta( $post->ID, '_wp_page_template', true );
|
||||
if ( ! $template || 'default' == $template )
|
||||
|
||||
if ( ! $template || 'default' == $template ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $template;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user