Home>

Detailed function meaningThe wp_title function is used to display the title of the page.As in the article page,Then display the article title;On the category page,The category name is displayed,But on the homepage index,The function will not display anything.

It's a bit like the adaptive usage of the two functions get_the_title and single_cat_title () in wordpress (automatically determine whether it is a page, article or category, archive, tag).

Function declarationIt's a bit long, I hope you can look at it patiently.Even if only once.

/**
 * display or retrieve page title for all areas of blog.
 *
 * by default, the page title will display the separator before the page title, * so that the blog title will be before the page title. this is not good for
 * title display, since the blog title shows up on most tabs and not what is
 * important, which is the page that the user is looking at.
 *
 * there are also seo benefits to having the blog title after or to the "right"
 * or the page title. However, it is mostly common sense to have the blog title
 * to the right with most browsers supporting tabs. you can achieve this by
 * using the seplocation parameter and setting the value to "right". this change
 * was 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 title, "right".
 * @return stringnull string on retrieve, null when displaying.
 * /
function wp_title ($sep="»", $display=true, $seplocation="") {
global $wpdb, $wp_locale;
$m=get_query_var ("m");
$year=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 post
if (is_single () || (is_home ()&&! is_front_page ()) || (is_page ()&&! is_front_page ())) {
 $title=single_post_title ("", false);
}
//if there "s a category or tag
if (is_category () || is_tag ()) {
 $title=single_term_title ("", false);
}
//if there "s a taxonomy
if (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 author
if (is_author ()) {
 $author=get_queried_object ();
 $title=$author->display_name;
}
//if there "s a post type archive
if (is_post_type_archive ())
 $title=post_type_archive_title ("", false);
//if there "s a month
if (is_archive ()&&! empty ($m)) {
 $my_year=substr ($m, 0, 4);
 $my_month=$wp_locale->get_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 year
if (is_archive () &&! empty ($year)) {
 $title=$year;
 if (! empty ($monthnum))
 $title.=$t_sep. $wp_locale->get_month ($monthnum);
 if (! empty ($day))
 $title.=$t_sep. zeroise ($day, 2);
}
//if it "s a search
if (is_search ()) {
 /* translators:1:separator, 2:search phrase * /
$title=sprintf (__ ("search results%1 $s%2 $s"), $t_sep, strip_tags ($search));
}
//if it "s a 404 page
if (is_404 ()) {
 $title=__ ("page not found");
}
$prefix="";
if (! empty ($title))
 $prefix="$sep";
//determines position of the separator and direction of the breadcrumb
if ("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:separator; $echo:whether to display; $seplocation:the location of the separator (left or right,Only accept "right", if it is not right, it will be judged as left automatically)

to sum up

There are many functions with the same function in wordpress,They are continuously encapsulated from the basic layer to the advanced level and finally reach the use layer.Of course if we need some flexible usage,We can use the function in the middle layer directly,If we are lazy, we can directly use the highest-level functions,Such as this function wp_title, in fact, we look at this function from the source code, wp has judged for us various types of pages such as categories, tags, articles, archives, authors, pages, etc.And call different title functions according to different pages to achieve the purpose.

if there is time,You can delve into the following functions,More flexible seo

single_post_title function to extract title from post page

single_term_title tag (tag), cat (category), date, function to extract the title Similar to single_cat_title () function

get_queried_object function for author page extraction object (the author name in the object)

post_type_archive_title () profile and so on

What are you waiting for?

go go go!

php
  • Previous Android programming development music player example
  • Next Summary of problems encountered in C # call JavaWebservice service
  • Trends