A detailed description of the use of Wp_title functions in WordPress development

Source: Internet
Author: User
Wp_title function in WordPress is used to display the article, page, category and so on the title of a function, but on the index page, the function will not show anything. This function has been used in the official WordPress theme, but the function is always overlooked in many custom themes.

Explanation of function meaning
The Wp_title function is used to display the title of the page, such as the article title on the article page, and the category name on the category page, but on the first page index, the function will not show anything.
A bit like the Get_the_title and Single_cat_title in WordPress () The adaptive usage of the two functions (automatic judgment is the page, the article or the classification, the archive, the label).

function declaration
A bit long, I hope you can look patiently, even if only then once.

/** * Display or retrieve page title for all areas of blogs. * By default, the page title would display the separator before the page title, * So, the blog title would be before T He page title.  This isn't good for * title display, since the blog title shows up to most tabs and isn't what's * important, which is the page, the user is looking at. * * There is also SEO benefits to have the blog title after or to the "right" * or the page title. However, it's mostly common sense to has the blog title * to the right and most browsers supporting tabs. You can achieve this by * using the seplocation parameter and setting the value into ' right '. This change * were introduced around 2.5.0, in case backwards compatibility of themes is * important. * * @since 1.0.0 * * @param string $sep Optional, default is '» '. How to separate the various items within the page title. * @param bool $display Optional, default is true. Whether to display or retrieve title. * @param string $seplocation Optional. Direction to display the title, ' Right '. * @return String|null string on retrieve, null when displaying. */function wp_title ($sep = '» ', $display = true, $seplocation = ') {global $wpdb, $wp _locale; $m = Get_query_var (' m '); $ye AR = Get_query_var (' year '), $monthnum = Get_query_var (' Monthnum '); $day = Get_query_var (' Day '); $search = Get_query_var (' S '); $title = '; $t _sep = '%wp_titile_sep% '; Temporary separator, for accurate flipping, if necessary//if there is a postif (Is_single () | | (Is_home () &&!is_front_page ()) | | (Is_page () &&!is_front_page ())) {$title = Single_post_title (', false);} If there's a category or Tagif (Is_category () | | Is_tag ()) {$title = Single_term_title (', false);}//If there ' s A taxonomyif (Is_tax ()) {$term = Get_queried_object (), $tax = get_taxonomy ($term->taxonomy); $title = Single_term_ Title ($tax->labels->name. $t _sep, false);} If there ' s an Authorif (Is_author ()) {$author = Get_queried_object ();$title = $author->display_name;} If there ' a post type Archiveif (Is_post_type_archive ()) $title = Post_type_archive_title (', false); If there ' s a Monthif (is_archive () &&!empty ($m)) {$my _year = substr ($m, 0, 4); $my _month = $wp _locale->g Et_month (substr ($m, 4, 2)); $my _day = intval (substr ($m, 6, 2)); $title = $my _year. ($my _month? $t _sep. $my _month: "). ($my _day? $t _sep. $my _day: ');} If there ' s a Yearif (is_archive () &&!empty ($year)) {$title = $year; if (!empty ($monthnum)) $title. = $t _s Ep. $WP _locale->get_month ($monthnum); if (!empty ($day)) $title. = $t _sep. Zeroise ($day, 2);} If It's a Searchif (Is_search ()) {/* Translators:1: Separator, 2:search phrase */$title = sprintf (__ (' Search Resul TS%1 $ s%2$s '), $t _sep, Strip_tags ($search));}  If it's a 404 Pageif (is_404 ()) {$title = __ (' Page not Found ');} $prefix = '; if (!empty ($title)) $prefix = "$sep "; Determines position of the separator and directiOn the breadcrumbif ("right" = = $seplocation) {//Sep on right, so reverse the Order$title_array = explode ($t _sep, $title) $title _array = Array_reverse ($title _array), $title = Implode ("$sep", $title _array). $prefix;} else {$title _array = explode ($t _sep, $title); $title = $prefix. Implode ("$sep", $title _array);} $title = apply_filters (' Wp_title ', $title, $sep, $seplocation); Send it out if ($display) echo $title; else return $title; }

Usage

<?php Wp_title ($sep, $echo, $seplocation);?>

Detailed parameters

    • $sep: delimiter;
    • $echo: whether it is displayed;
    • $seplocation: Where the delimiter is located (left or right, only ' right ', if it is not automatically determined to be left)

Summarize

WordPress functions in the same function have a lot, are from the grass-roots to the advanced continuous encapsulation finally reached the use layer, of course, if we need some flexible usage, we can directly use the middle layer of the function, if we lazy, we can directly use the highest level of the function, such as this function wp_ Title, in fact, this function we from the source code, WP for our classification, tags, articles, archives, authors, pages and other types of pages are judged, and according to different pages call different title function to achieve the purpose.
If you have time, you can study the following several functions in depth, more flexible to do SEO
Single_post_title function for extracting titles from article pages
Single_term_title tag (tag), Cat (category), date, extract title function similar to the Single_cat_title () function
Get_queried_object The author page extracts the object's function (the object has the author name)
Post_type_archive_title () files and so on extract the title function
What are you waiting for?
Go Go Go!

The above describes the use of Wp_title functions in the development of WordPress, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.