Look for single-.php templates. Add single- class to get_body_class(). Props ptahdunbar. see #12105

git-svn-id: http://svn.automattic.com/wordpress/trunk@13032 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2010-02-08 22:05:05 +00:00
parent 152304f1e1
commit 4ddde1ab9d
3 changed files with 29 additions and 23 deletions

View File

@ -392,14 +392,17 @@ function get_body_class( $class = '' ) {
$classes[] = 'error404'; $classes[] = 'error404';
if ( is_single() ) { if ( is_single() ) {
$postID = $wp_query->get_queried_object_id(); $post_id = $wp_query->get_queried_object_id();
$post = $wp_query->get_queried_object();
$classes[] = 'single postid-' . $postID; $classes[] = 'single';
$classes[] = 'single-' . sanitize_html_class($post->post_type, $post_id);
$classes[] = 'postid-' . $post_id;
if ( is_attachment() ) { if ( is_attachment() ) {
$mime_type = get_post_mime_type($postID); $mime_type = get_post_mime_type($post_id);
$mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' ); $mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' );
$classes[] = 'attachmentid-' . $postID; $classes[] = 'attachmentid-' . $post_id;
$classes[] = 'attachment-' . str_replace($mime_prefix, '', $mime_type); $classes[] = 'attachment-' . str_replace($mime_prefix, '', $mime_type);
} }
} elseif ( is_archive() ) { } elseif ( is_archive() ) {
@ -419,13 +422,13 @@ function get_body_class( $class = '' ) {
} elseif ( is_page() ) { } elseif ( is_page() ) {
$classes[] = 'page'; $classes[] = 'page';
$pageID = $wp_query->get_queried_object_id(); $page_id = $wp_query->get_queried_object_id();
$post = get_page($pageID); $post = get_page($page_id);
$classes[] = 'page-id-' . $pageID; $classes[] = 'page-id-' . $page_id;
if ( $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'page' AND post_status = 'publish' LIMIT 1", $pageID) ) ) if ( $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'page' AND post_status = 'publish' LIMIT 1", $page_id) ) )
$classes[] = 'page-parent'; $classes[] = 'page-parent';
if ( $post->post_parent ) { if ( $post->post_parent ) {
@ -434,7 +437,7 @@ function get_body_class( $class = '' ) {
} }
if ( is_page_template() ) { if ( is_page_template() ) {
$classes[] = 'page-template'; $classes[] = 'page-template';
$classes[] = 'page-template-' . sanitize_html_class( str_replace( '.', '-', get_post_meta( $pageID, '_wp_page_template', true ) ), '' ); $classes[] = 'page-template-' . sanitize_html_class( str_replace( '.', '-', get_post_meta( $page_id, '_wp_page_template', true ) ), '' );
} }
} elseif ( is_search() ) { } elseif ( is_search() ) {
if ( !empty($wp_query->posts) ) if ( !empty($wp_query->posts) )

View File

@ -2549,28 +2549,27 @@ class WP_Query {
* @return object * @return object
*/ */
function get_queried_object() { function get_queried_object() {
if (isset($this->queried_object)) { if ( isset($this->queried_object) )
return $this->queried_object; return $this->queried_object;
}
$this->queried_object = NULL; $this->queried_object = NULL;
$this->queried_object_id = 0; $this->queried_object_id = 0;
if ($this->is_category) { if ( $this->is_category ) {
$cat = $this->get('cat'); $cat = $this->get('cat');
$category = &get_category($cat); $category = &get_category($cat);
if ( is_wp_error( $category ) ) if ( is_wp_error( $category ) )
return NULL; return NULL;
$this->queried_object = &$category; $this->queried_object = &$category;
$this->queried_object_id = (int) $cat; $this->queried_object_id = (int) $cat;
} else if ($this->is_tag) { } elseif ( $this->is_tag ) {
$tag_id = $this->get('tag_id'); $tag_id = $this->get('tag_id');
$tag = &get_term($tag_id, 'post_tag'); $tag = &get_term($tag_id, 'post_tag');
if ( is_wp_error( $tag ) ) if ( is_wp_error( $tag ) )
return NULL; return NULL;
$this->queried_object = &$tag; $this->queried_object = &$tag;
$this->queried_object_id = (int) $tag_id; $this->queried_object_id = (int) $tag_id;
} else if ($this->is_tax) { } elseif ( $this->is_tax ) {
$tax = $this->get('taxonomy'); $tax = $this->get('taxonomy');
$slug = $this->get('term'); $slug = $this->get('term');
$term = &get_terms($tax, array('slug'=>$slug)); $term = &get_terms($tax, array('slug'=>$slug));
@ -2579,16 +2578,16 @@ class WP_Query {
$term = $term[0]; $term = $term[0];
$this->queried_object = $term; $this->queried_object = $term;
$this->queried_object_id = $term->term_id; $this->queried_object_id = $term->term_id;
} else if ($this->is_posts_page) { } elseif ( $this->is_posts_page ) {
$this->queried_object = & get_page(get_option('page_for_posts')); $this->queried_object = & get_page(get_option('page_for_posts'));
$this->queried_object_id = (int) $this->queried_object->ID; $this->queried_object_id = (int) $this->queried_object->ID;
} else if ($this->is_single) { } elseif ( $this->is_single ) {
$this->queried_object = $this->post; $this->queried_object = $this->post;
$this->queried_object_id = (int) $this->post->ID; $this->queried_object_id = (int) $this->post->ID;
} else if ($this->is_page) { } elseif ( $this->is_page ) {
$this->queried_object = $this->post; $this->queried_object = $this->post;
$this->queried_object_id = (int) $this->post->ID; $this->queried_object_id = (int) $this->post->ID;
} else if ($this->is_author) { } elseif ( $this->is_author ) {
$author_id = (int) $this->get('author'); $author_id = (int) $this->get('author');
$author = get_userdata($author_id); $author = get_userdata($author_id);
$this->queried_object = $author; $this->queried_object = $author;

View File

@ -860,7 +860,7 @@ function get_home_template() {
function get_page_template() { function get_page_template() {
global $wp_query; global $wp_query;
$id = (int) $wp_query->post->ID; $id = (int) $wp_query->get_queried_object_id();
$template = get_post_meta($id, '_wp_page_template', true); $template = get_post_meta($id, '_wp_page_template', true);
$pagename = get_query_var('pagename'); $pagename = get_query_var('pagename');
@ -909,7 +909,11 @@ function get_search_template() {
* @return string * @return string
*/ */
function get_single_template() { function get_single_template() {
return get_query_template('single'); global $wp_query;
$object = $wp_query->get_queried_object();
$templates = array('single-' . $object->post_type . '.php', 'single.php');
return apply_filters('single_template', locate_template($templates));
} }
/** /**
@ -974,11 +978,11 @@ function get_comments_popup_template() {
* @return string The template filename if one is located. * @return string The template filename if one is located.
*/ */
function locate_template($template_names, $load = false) { function locate_template($template_names, $load = false) {
if (!is_array($template_names)) if ( !is_array($template_names) )
return ''; return '';
$located = ''; $located = '';
foreach($template_names as $template_name) { foreach ( $template_names as $template_name ) {
if ( file_exists(STYLESHEETPATH . '/' . $template_name)) { if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
$located = STYLESHEETPATH . '/' . $template_name; $located = STYLESHEETPATH . '/' . $template_name;
break; break;
@ -988,7 +992,7 @@ function locate_template($template_names, $load = false) {
} }
} }
if ($load && '' != $located) if ( $load && '' != $located )
load_template($located); load_template($located);
return $located; return $located;