Optimize single_post_title(), single_tag_title() & single_cat_title() to use WP_Query globals if available. Removes an extra DB Query in single_post_title() in most cases.

git-svn-id: http://svn.automattic.com/wordpress/trunk@13143 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
dd32 2010-02-14 07:38:00 +00:00
parent 658c91ade3
commit 0b8b9ef52a
1 changed files with 49 additions and 35 deletions

View File

@ -608,21 +608,21 @@ function wp_title($sep = '»', $display = true, $seplocation = '') {
* @return string|null Title when retrieving, null when displaying or failure. * @return string|null Title when retrieving, null when displaying or failure.
*/ */
function single_post_title($prefix = '', $display = true) { function single_post_title($prefix = '', $display = true) {
global $wpdb; global $wpdb, $post;
$p = get_query_var('p'); if ( ! $post ) {
$name = get_query_var('name'); $p = get_query_var('p');
$name = get_query_var('name');
if ( intval($p) || '' != $name ) { if ( intval($p) || '' != $name ) {
if ( !$p ) if ( !$p )
$p = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_name = %s", $name)); $p = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_name = %s", $name));
$post = & get_post($p); $post = & get_post($p);
$title = $post->post_title; }
$title = apply_filters('single_post_title', $title);
if ( $display )
echo $prefix.strip_tags($title);
else
return strip_tags($title);
} }
$title = apply_filters('single_post_title', $post->post_title, $post);
if ( $display )
echo $prefix . strip_tags($title);
else
return strip_tags($title);
} }
/** /**
@ -643,17 +643,24 @@ function single_post_title($prefix = '', $display = true) {
* @return string|null Title when retrieving, null when displaying or failure. * @return string|null Title when retrieving, null when displaying or failure.
*/ */
function single_cat_title($prefix = '', $display = true ) { function single_cat_title($prefix = '', $display = true ) {
$cat = intval( get_query_var('cat') ); global $wp_query;
if ( !empty($cat) && !(strtoupper($cat) == 'ALL') ) { $cat = $wp_query->queried_object;
$my_cat_name = apply_filters('single_cat_title', get_the_category_by_ID($cat));
if ( !empty($my_cat_name) ) { if ( is_tag() ) {
if ( $display )
echo $prefix.strip_tags($my_cat_name);
else
return strip_tags($my_cat_name);
}
} else if ( is_tag() ) {
return single_tag_title($prefix, $display); return single_tag_title($prefix, $display);
} elseif ( !empty($cat->name) ) {
$cat = $cat->name;
} else {
$cat = intval( get_query_var('cat') );
if ( !empty($cat) )
$cat = get_the_category_by_ID($cat);
}
$my_cat_name = apply_filters('single_cat_title', $cat);
if ( !empty($my_cat_name) ) {
if ( $display )
echo $prefix . strip_tags($my_cat_name);
else
return strip_tags($my_cat_name);
} }
} }
@ -678,20 +685,27 @@ function single_tag_title($prefix = '', $display = true ) {
if ( !is_tag() ) if ( !is_tag() )
return; return;
$tag_id = intval( get_query_var('tag_id') ); global $wp_query;
$tag = $wp_query->queried_object;
if ( !empty($tag_id) ) { if ( !empty($tag->name) ) {
$my_tag = &get_term($tag_id, 'post_tag', OBJECT, 'display'); $tag = sanitize_term($tag, 'post_tag', 'display');
if ( is_wp_error( $my_tag ) ) } else {
return false; $tag_id = intval( get_query_var('tag_id') );
$my_tag_name = apply_filters('single_tag_title', $my_tag->name); if ( !empty($tag_id) ) {
if ( !empty($my_tag_name) ) { $tag = &get_term($tag_id, 'post_tag', OBJECT, 'display');
if ( $display ) if ( is_wp_error( $tag ) )
echo $prefix . $my_tag_name; return false;
else
return $my_tag_name;
} }
} }
$my_tag_name = apply_filters('single_tag_title', $tag->name);
if ( !empty($my_tag_name) ) {
if ( $display )
echo $prefix . $my_tag_name;
else
return $my_tag_name;
}
} }
/** /**