For a WordPress website, its front page is ever-changing, and different pages must have different content and titles. If all the pages of a WordPress website have the same title, it is very unfriendly to search engines. So, in During the development of wordpress website theme template, how to realize different pages calling different titles ? This is not difficult. We can do it in two ways.
Method 1: Through judgment, different pages of the wordpress website call different titles.
Wordpress provides judgment functions for template pages. We can use these functions to judge the current page, and then call these page title functions. For example, the category page calls the category page title function; The article page calls the article page title function. The codes are as follows:
If (is_home()) {//If it is the first page, call the first page title
bloginfo(name);
}Elseif (is_single() | | is_page()) {//If it is an article detail page or a single page
the_title(); // Title of the article and page
echo " - ";
bloginfo(name);
}else{
single_cat_title(, false); // Category directory and title of tag page
echo " - ";
bloginfo(name);
}
Through the above judgment, we can achieve: Wordpress website The home page directly displays the title of the home page; If it is a single page of articles and pages, use the_title() to call their titles; For the category and tag tabs, use single_cat_title() to call their titles.
Method 2: Use the title function wp_title() of wordpress to implement.
Wp_title() is wordpress The provided website title function can call the corresponding title on other website pages except the homepage of the website, which also achieves the effect of method 1. Let's look at this function first.
wp_title( $sep, $display, $seplocation );
From the above code, we can see that the wp_title() function can have three parameters:
$sep: string type data, optional. Here is the separator for the front page title of the wordpress website. The default value is ». If you want to use other separators, you can use this parameter$ Display: Boolean data type, optional. This parameter means whether to print the title to the page. The default value is true, indicating display. If you only want to assign values to variables, you can set it to false$ Seplacation: string type data, optional. The function of this parameter is to display the separator on the left by default. If you want to display the separator on the right of the title, you can set it to right.
Case:
<? php
wp_title(" - ",true,right);
bloginfo("name"); echo " - ";
bloginfo("description");
?>
When the wp_title() is on the home page, it will not display any content because the data cannot be called; And it is in the rest of the wordpress website Template The page can play a role. The article title will be called on the article template page, the page title will be called on the page single page template page, the classification directory title will be called on the website classification page, and the tag title will be called on the tags page. That is to say, wordpress is already done inside the wp_title() function Site Template Then, call the titles of different pages according to different website template pages.
If you like my article, please click the "Follow" button to follow me. I will publish new content regularly every day. These are my views. If you have different views, please comment.