<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[15598] trunk/wp-includes/general-template.php: Clean up single_cat_title()
, single_tag_title() and wp_title().</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/15598">15598</a></dd>
<dt>Author</dt> <dd>scribu</dd>
<dt>Date</dt> <dd>2010-09-08 16:21:13 +0000 (Wed, 08 Sep 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Clean up single_cat_title(), single_tag_title() and wp_title(). Fixes <a href="http://trac.wordpress.org/ticket/14161">#14161</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesgeneraltemplatephp">trunk/wp-includes/general-template.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesgeneraltemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/general-template.php (15597 => 15598)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/general-template.php        2010-09-08 12:15:50 UTC (rev 15597)
+++ trunk/wp-includes/general-template.php        2010-09-08 16:21:13 UTC (rev 15598)
</span><span class="lines">@@ -516,11 +516,6 @@
</span><span class="cx"> function wp_title($sep = '&raquo;', $display = true, $seplocation = '') {
</span><span class="cx">         global $wpdb, $wp_locale, $wp_query;
</span><span class="cx">
</span><del>-        $cat = get_query_var('cat');
-        $tag = get_query_var('tag_id');
-        $category_name = get_query_var('category_name');
-        $author = get_query_var('author');
-        $author_name = get_query_var('author_name');
</del><span class="cx">         $m = get_query_var('m');
</span><span class="cx">         $year = get_query_var('year');
</span><span class="cx">         $monthnum = get_query_var('monthnum');
</span><span class="lines">@@ -530,51 +525,38 @@
</span><span class="cx">
</span><span class="cx">         $t_sep = '%WP_TITILE_SEP%'; // Temporary separator, for accurate flipping, if necessary
</span><span class="cx">
</span><del>-        // If there's a category
-        if ( !empty($cat) ) {
-                        // category exclusion
-                        if ( !stristr($cat,'-') )
-                                $title = apply_filters('single_cat_title', get_the_category_by_ID($cat));
-        } elseif ( !empty($category_name) ) {
-                if ( stristr($category_name,'/') ) {
-                                $category_name = explode('/',$category_name);
-                                if ( $category_name[count($category_name)-1] )
-                                        $category_name = $category_name[count($category_name)-1]; // no trailing slash
-                                else
-                                        $category_name = $category_name[count($category_name)-2]; // there was a trailling slash
-                }
-                $cat = get_term_by('slug', $category_name, 'category', OBJECT, 'display');
-                if ( $cat )
-                        $title = apply_filters('single_cat_title', $cat->name);
</del><ins>+        // If there is a post
+        if ( is_single() || ( is_home() && !is_front_page() ) || ( is_page() && !is_front_page() ) ) {
+                $title = single_post_title( '', false );
</ins><span class="cx">         }
</span><span class="cx">
</span><del>-        if ( !empty($tag) ) {
-                $tag = get_term($tag, 'post_tag', OBJECT, 'display');
-                if ( is_wp_error( $tag ) )
-                        return $tag;
-                if ( ! empty($tag->name) )
-                        $title = apply_filters('single_tag_title', $tag->name);
</del><ins>+        // If there's a category or tag
+        if ( is_category() || is_tag() ) {
+                $title = single_term_title( '', false );
</ins><span class="cx">         }
</span><span class="cx">
</span><ins>+        // If there's a taxonomy
+        if ( is_tax() ) {
+                $tax = get_taxonomy( get_query_var('taxonomy') );
+                $title = single_term_title( $tax->labels->name . $t_sep, false );
+        }
+
</ins><span class="cx">         // If there's an author
</span><del>-        if ( !empty($author) ) {
-                $title = get_userdata($author);
-                $title = $title->display_name;
</del><ins>+        if ( is_author() ) {
+                $author = $wp_query->get_queried_object();
+                $title = $author->display_name;
</ins><span class="cx">         }
</span><del>-        if ( !empty($author_name) ) {
-                // We do a direct query here because we don't cache by nicename.
-                $title = $wpdb->get_var($wpdb->prepare("SELECT display_name FROM $wpdb->users WHERE user_nicename = %s", $author_name));
-        }
</del><span class="cx">
</span><span class="cx">         // If there's a month
</span><del>-        if ( !empty($m) ) {
</del><ins>+        if ( is_archive() && !empty($m) ) {
</ins><span class="cx">                 $my_year = substr($m, 0, 4);
</span><span class="cx">                 $my_month = $wp_locale->get_month(substr($m, 4, 2));
</span><span class="cx">                 $my_day = intval(substr($m, 6, 2));
</span><span class="cx">                 $title = $my_year . ($my_month ? $t_sep . $my_month : "") . ($my_day ? $t_sep . $my_day : "");
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        if ( !empty($year) ) {
</del><ins>+        // If there's a year
+        if ( is_archive() && !empty($year) ) {
</ins><span class="cx">                 $title = $year;
</span><span class="cx">                 if ( !empty($monthnum) )
</span><span class="cx">                         $title .= $t_sep . $wp_locale->get_month($monthnum);
</span><span class="lines">@@ -582,27 +564,13 @@
</span><span class="cx">                         $title .= $t_sep . zeroise($day, 2);
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        // If there is a post
-        if ( is_single() || ( is_home() && !is_front_page() ) || ( is_page() && !is_front_page() ) ) {
-                $post = $wp_query->get_queried_object();
-                $title = apply_filters( 'single_post_title', $post->post_title );
-        }
-
-        // If there's a taxonomy
-        if ( is_tax() ) {
-                $taxonomy = get_query_var( 'taxonomy' );
-                $tax = get_taxonomy( $taxonomy );
-                $term = $wp_query->get_queried_object();
-                $term = $term->name;
-                $title = $tax->labels->name . $t_sep . $term;
-        }
-
-        //If it's a search
</del><ins>+        // If it's a search
</ins><span class="cx">         if ( is_search() ) {
</span><span class="cx">                 /* translators: 1: separator, 2: search phrase */
</span><span class="cx">                 $title = sprintf(__('Search Results %1$s %2$s'), $t_sep, strip_tags($search));
</span><span class="cx">         }
</span><span class="cx">
</span><ins>+        // If it's a 404 page
</ins><span class="cx">         if ( is_404() ) {
</span><span class="cx">                 $title = __('Page not found');
</span><span class="cx">         }
</span><span class="lines">@@ -684,23 +652,8 @@
</span><span class="cx"> * @param bool $display Optional, default is true. Whether to display or retrieve title.
</span><span class="cx"> * @return string|null Title when retrieving, null when displaying or failure.
</span><span class="cx"> */
</span><del>-function single_cat_title($prefix = '', $display = true ) {
-        global $wp_query;
-
-        if ( is_tag() )
-                return single_tag_title($prefix, $display);
-
-        if ( !is_category() )
-                return;
-
-        $cat = $wp_query->get_queried_object();
-        $my_cat_name = apply_filters('single_cat_title', $cat->name);
-        if ( !empty($my_cat_name) ) {
-                if ( $display )
-                        echo $prefix . $my_cat_name;
-                else
-                        return $my_cat_name;
-        }
</del><ins>+function single_cat_title( $prefix = '', $display = true ) {
+        return single_term_title( $prefix, $display );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><span class="lines">@@ -720,23 +673,51 @@
</span><span class="cx"> * @param bool $display Optional, default is true. Whether to display or retrieve title.
</span><span class="cx"> * @return string|null Title when retrieving, null when displaying or failure.
</span><span class="cx"> */
</span><del>-function single_tag_title($prefix = '', $display = true ) {
</del><ins>+function single_tag_title( $prefix = '', $display = true ) {
+        return single_term_title( $prefix, $display );
+}
+
+/**
+ * Display or retrieve page title for taxonomy term archive.
+ *
+ * Useful for taxonomy term template files for displaying the taxonomy term page title.
+ * It has less overhead than {@link wp_title()}, because of its limited implementation.
+ *
+ * It does not support placing the separator after the title, but by leaving the
+ * prefix parameter empty, you can set the title separator manually. The prefix
+ * does not automatically place a space between the prefix, so if there should
+ * be a space, the parameter value will need to have it at the end.
+ *
+ * @since 3.1.0
+ *
+ * @param string $prefix Optional. What to display before the title.
+ * @param bool $display Optional, default is true. Whether to display or retrieve title.
+ * @return string|null Title when retrieving, null when displaying or failure.
+ */
+function single_term_title( $prefix = '', $display = true ) {
</ins><span class="cx">         global $wp_query;
</span><del>-        if ( !is_tag() )
</del><ins>+
+        $term = $wp_query->get_queried_object();
+
+        if ( !$term )
</ins><span class="cx">                 return;
</span><span class="cx">
</span><del>-        $tag = $wp_query->get_queried_object();
</del><ins>+        if ( is_category() )
+                $term_name = apply_filters( 'single_cat_title', $term->name );
+        elseif ( is_tag() )
+                $term_name = apply_filters( 'single_tag_title', $term->name );
+        elseif ( is_term() )
+                $term_name = apply_filters( 'single_term_title', $term->name );
+        else
+                return;
</ins><span class="cx">
</span><del>-        if ( ! $tag )
</del><ins>+        if ( empty( $term_name ) )
</ins><span class="cx">                 return;
</span><span class="cx">
</span><del>-        $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;
-        }
</del><ins>+        if ( $display )
+                echo $prefix . $term_name;
+
+        return $term_name;
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span></span></pre>
</div>
</div>
</body>
</html>