Add keywords and descriptions to the WordPress blog homepage/topic/article

Source: Internet
Author: User
Tags php file rtrim tag name wordpress blog

Open the blog root directory, the header. Php file under the wp-content/themes/your topic directory, in

The code is as follows: Copy code
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

Add the following:

The code is as follows: Copy code
<? Php if (is_home () {?>
<Meta name = "keywords" content = "keyword of your website"/>
<Meta name = "description" content = "your website description"/>
<Link rel = "canonical" href = "<? Php echo get_settings ('Home');?> "/>
<? Php }?>
<? Php if (is_single () {?>
<Meta name = "keywords" content = "<? Php $ key = "keywords"; echo get_post_meta ($ post-> ID, $ key, true);?> "/>
<Meta name = "description" content = "<? Php $ key = "description"; echo get_post_meta ($ post-> ID, $ key, true);?> "/>
<Link rel = "canonical" href = "<? Php echo get_permalink ($ post-> ID);?> "/>
<? Php }?>

You can modify the code as needed, provided that you understand what it means.
Before you finish, modify the "keyword of your website" for the homepage keyword you want and modify the "description of your website" for the homepage description you want!
The following is the article page. When each article is published, add two custom domains in the custom domain:
Name 1: keywords; value 1: keyword of this page.
Name 2: description; value 2: description of the page.


 

In this way, the corresponding keyword and description will also appear on your article page ~~~ You can check whether the keywords and description in the code on this page are the values on the image.

Next, let's look at a blog homepage/topic/article to add keywords and describe instances.

Let's open the article above. The first and second steps are taken and the third steps are different. Let's take a look at the following:

3. Open header. php in the editor and add the following code to any location between

The code is as follows: Copy code
<? Php
// For The homepage
If (is_home ()){
$ Keywords = "modify the keywords on the homepage of your website ";
$ Description = "modify the description of your website homepage ";
}
// If it is an article page
Elseif (is_single ()){
// Add keywords on the document page by default
$ Keywords = get_post_meta ($ post-> ID, "keywords", true );
// If it is null, use the tag as the keyword
If ($ keywords = ""){
$ Tags = wp_get_post_tags ($ post-> ID );
Foreach ($ tags as $ tag ){
$ Keywords = $ keywords. $ tag-> name .",";
  }
// Remove the last one,
$ Keywords = rtrim ($ keywords ,',');
 }
// Add description on the document page by default
$ Description = get_post_meta ($ post-> ID, "description", true );
// If it is null, use the first 100 words of the article as the description
If ($ description = ""){
If ($ post-> post_excerpt ){
$ Description = $ post-> post_excerpt;
} Else {
$ Description = mb_strimwidth (strip_tags (apply_filters ('The _ content', $ post-> post_content), 0,200 );
  }
 }
}
// For a page, use the keywords and descriptions added to the page.
Elseif (is_page ()){
$ Keywords = get_post_meta ($ post-> ID, "keywords", true );
$ Description = get_post_meta ($ post-> ID, "description", true );
}
// For the category page, use the category name as the keyword and the category description as the description
Elseif (is_category ()){
$ Keywords = single_cat_title ('', false );
$ Description = category_description ();
}
// For a tab, use the tag name as the keyword and the tag description as the description.
Elseif (is_tag ()){
$ Keywords = single_tag_title ('', false );
$ Description = tag_description ();
}
// Remove two spaces
$ Keywords = trim (strip_tags ($ keywords ));
$ Description = trim (strip_tags ($ description ));
?>
<Meta name = "keywords" content = "<? Php echo $ keywords;?> "/>
<Meta name = "description" content = "<? Php echo $ description;?> "/>

I have added all the major comments this time. I think you should understand it?

There are several more judgments on the article page. By default, the keyword is added to the document page. If it is null, the tag is used as the keyword. By default, the description is added on the document page. If the description is empty, the abstract is used. If the description is empty, the first 100 words in the document are used as the description.

If you see this, I believe someone will think it is troublesome to download the php file and change functions. php! And do not want to manually add this information, all of which are automatic. Okay, the lazy approach will be announced below.

This method only needs to modify the code once and open the header in the editor. php: add the following code anywhere between

The code is as follows: Copy code
<? Php
If (is_home ()){
$ Keywords = "modify the keywords on the homepage of your website ";
$ Description = "modify the description of your website homepage ";
}
Elseif (is_single ()){
$ Tags = wp_get_post_tags ($ post-> ID );
Foreach ($ tags as $ tag ){
$ Keywords = $ keywords. $ tag-> name .",";
 }
$ Keywords = rtrim ($ keywords ,',');
If ($ post-> post_excerpt ){
$ Description = $ post-> post_excerpt;
} Else {
$ Description = mb_strimwidth (strip_tags (apply_filters ('The _ content', $ post-> post_content), 0,200 );
 }
}
Elseif (is_page ()){
$ Keywords = get_post_meta ($ post-> ID, "keywords", true );
$ Description = get_post_meta ($ post-> ID, "description", true );
}
Elseif (is_category ()){
$ Keywords = single_cat_title ('', false );
$ Description = category_description ();
}
Elseif (is_tag ()){
$ Keywords = single_tag_title ('', false );
$ Description = tag_description ();
}
$ Keywords = trim (strip_tags ($ keywords ));
$ Description = trim (strip_tags ($ description ));
?>
<Meta name = "keywords" content = "<? Php echo $ keywords;?> "/>
<Meta name = "description" content = "<? Php echo $ description;?> "/>

OK. The keywords and descriptions of the five pages are all automatic once. I was just preparing to write this method as a plug-in. After reading the plug-in tutorial for half a day, I still cannot figure it out.


Method 3: compare the instance


The cow found a solution from the Internet, which is a piece of code for boiled fish. The code is as follows:

The code is as follows: Copy code
<? If (is_home ()){
$ Description = "website homepage description ";
$ Keywords = "website keywords ";
} Elseif (is_single ()){
If ($ post-> post_excerpt ){
$ Description = $ post-> post_excerpt;
} Else {
$ Description = substr (strip_tags ($ post-> post_content), 0,220 );
    }
 
$ Keywords = "";
$ Tags = wp_get_post_tags ($ post-> ID );
Foreach ($ tags as $ tag ){
$ Keywords = $ keywords. $ tag-> name .",";
    }
}
?>
<Meta name = "keywords" content = "<? = $ Keywords?> "/>
<Meta name = "description" content = "<? = $ Description?> "/>

 

The header file of the topic may contain

The code is as follows: Copy code

<Meta name = "keywords" content = "<? = $ Keywords?> "/>
<Meta name = "description" content = "<? = $ Description?> "/>

Or similar text, just replace it. If not, add it to the title of the head file.

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.