WordPress.org

Make WordPress Core

Changeset 15598


Ignore:
Timestamp:
09/08/2010 04:21:13 PM (8 years ago)
Author:
scribu
Message:

Clean up single_cat_title(), single_tag_title() and wp_title(). Fixes #14161

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/general-template.php

    r15590 r15598  
    517517    global $wpdb, $wp_locale, $wp_query;
    518518
    519     $cat = get_query_var('cat');
    520     $tag = get_query_var('tag_id');
    521     $category_name = get_query_var('category_name');
    522     $author = get_query_var('author');
    523     $author_name = get_query_var('author_name');
    524519    $m = get_query_var('m');
    525520    $year = get_query_var('year');
     
    531526    $t_sep = '%WP_TITILE_SEP%'; // Temporary separator, for accurate flipping, if necessary
    532527
    533     // If there's a category
    534     if ( !empty($cat) ) {
    535             // category exclusion
    536             if ( !stristr($cat,'-') )
    537                 $title = apply_filters('single_cat_title', get_the_category_by_ID($cat));
    538     } elseif ( !empty($category_name) ) {
    539         if ( stristr($category_name,'/') ) {
    540                 $category_name = explode('/',$category_name);
    541                 if ( $category_name[count($category_name)-1] )
    542                     $category_name = $category_name[count($category_name)-1]; // no trailing slash
    543                 else
    544                     $category_name = $category_name[count($category_name)-2]; // there was a trailling slash
    545         }
    546         $cat = get_term_by('slug', $category_name, 'category', OBJECT, 'display');
    547         if ( $cat )
    548             $title = apply_filters('single_cat_title', $cat->name);
    549     }
    550 
    551     if ( !empty($tag) ) {
    552         $tag = get_term($tag, 'post_tag', OBJECT, 'display');
    553         if ( is_wp_error( $tag ) )
    554             return $tag;
    555         if ( ! empty($tag->name) )
    556             $title = apply_filters('single_tag_title', $tag->name);
     528    // If there is a post
     529    if ( is_single() || ( is_home() && !is_front_page() ) || ( is_page() && !is_front_page() ) ) {
     530        $title = single_post_title( '', false );
     531    }
     532
     533    // If there's a category or tag
     534    if ( is_category() || is_tag() ) {
     535        $title = single_term_title( '', false );
     536    }
     537
     538    // If there's a taxonomy
     539    if ( is_tax() ) {
     540        $tax = get_taxonomy( get_query_var('taxonomy') );
     541        $title = single_term_title( $tax->labels->name . $t_sep, false );
    557542    }
    558543
    559544    // If there's an author
    560     if ( !empty($author) ) {
    561         $title = get_userdata($author);
    562         $title = $title->display_name;
    563     }
    564     if ( !empty($author_name) ) {
    565         // We do a direct query here because we don't cache by nicename.
    566         $title = $wpdb->get_var($wpdb->prepare("SELECT display_name FROM $wpdb->users WHERE user_nicename = %s", $author_name));
     545    if ( is_author() ) {
     546        $author = $wp_query->get_queried_object();
     547        $title = $author->display_name;
    567548    }
    568549
    569550    // If there's a month
    570     if ( !empty($m) ) {
     551    if ( is_archive() && !empty($m) ) {
    571552        $my_year = substr($m, 0, 4);
    572553        $my_month = $wp_locale->get_month(substr($m, 4, 2));
     
    575556    }
    576557
    577     if ( !empty($year) ) {
     558    // If there's a year
     559    if ( is_archive() && !empty($year) ) {
    578560        $title = $year;
    579561        if ( !empty($monthnum) )
     
    583565    }
    584566
    585     // If there is a post
    586     if ( is_single() || ( is_home() && !is_front_page() ) || ( is_page() && !is_front_page() ) ) {
    587         $post = $wp_query->get_queried_object();
    588         $title = apply_filters( 'single_post_title', $post->post_title );
    589     }
    590 
    591     // If there's a taxonomy
    592     if ( is_tax() ) {
    593         $taxonomy = get_query_var( 'taxonomy' );
    594         $tax = get_taxonomy( $taxonomy );
    595         $term = $wp_query->get_queried_object();
    596         $term = $term->name;
    597         $title = $tax->labels->name . $t_sep . $term;
    598     }
    599 
    600     //If it's a search
     567    // If it's a search
    601568    if ( is_search() ) {
    602569        /* translators: 1: separator, 2: search phrase */
     
    604571    }
    605572
     573    // If it's a 404 page
    606574    if ( is_404() ) {
    607575        $title = __('Page not found');
     
    685653 * @return string|null Title when retrieving, null when displaying or failure.
    686654 */
    687 function single_cat_title($prefix = '', $display = true ) {
    688     global $wp_query;
    689 
    690     if ( is_tag() )
    691         return single_tag_title($prefix, $display);
    692 
    693     if ( !is_category() )
    694         return;
    695 
    696     $cat = $wp_query->get_queried_object();
    697     $my_cat_name = apply_filters('single_cat_title', $cat->name);
    698     if ( !empty($my_cat_name) ) {
    699         if ( $display )
    700             echo $prefix . $my_cat_name;
    701         else
    702             return $my_cat_name;
    703     }
     655function single_cat_title( $prefix = '', $display = true ) {
     656    return single_term_title( $prefix, $display );
    704657}
    705658
     
    721674 * @return string|null Title when retrieving, null when displaying or failure.
    722675 */
    723 function single_tag_title($prefix = '', $display = true ) {
     676function single_tag_title( $prefix = '', $display = true ) {
     677    return single_term_title( $prefix, $display );
     678}
     679
     680/**
     681 * Display or retrieve page title for taxonomy term archive.
     682 *
     683 * Useful for taxonomy term template files for displaying the taxonomy term page title.
     684 * It has less overhead than {@link wp_title()}, because of its limited implementation.
     685 *
     686 * It does not support placing the separator after the title, but by leaving the
     687 * prefix parameter empty, you can set the title separator manually. The prefix
     688 * does not automatically place a space between the prefix, so if there should
     689 * be a space, the parameter value will need to have it at the end.
     690 *
     691 * @since 3.1.0
     692 *
     693 * @param string $prefix Optional. What to display before the title.
     694 * @param bool $display Optional, default is true. Whether to display or retrieve title.
     695 * @return string|null Title when retrieving, null when displaying or failure.
     696 */
     697function single_term_title( $prefix = '', $display = true ) {
    724698    global $wp_query;
    725     if ( !is_tag() )
     699
     700    $term = $wp_query->get_queried_object();
     701
     702    if ( !$term )
    726703        return;
    727704
    728     $tag = $wp_query->get_queried_object();
    729 
    730     if ( ! $tag )
     705    if ( is_category() )
     706        $term_name = apply_filters( 'single_cat_title', $term->name );
     707    elseif ( is_tag() )
     708        $term_name = apply_filters( 'single_tag_title', $term->name );
     709    elseif ( is_term() )
     710        $term_name = apply_filters( 'single_term_title', $term->name );
     711    else
    731712        return;
    732713
    733     $my_tag_name = apply_filters('single_tag_title', $tag->name);
    734     if ( !empty($my_tag_name) ) {
    735         if ( $display )
    736             echo $prefix . $my_tag_name;
    737         else
    738             return $my_tag_name;
    739     }
     714    if ( empty( $term_name ) )
     715        return;
     716
     717    if ( $display )
     718        echo $prefix . $term_name;
     719
     720    return $term_name;
    740721}
    741722
Note: See TracChangeset for help on using the changeset viewer.