WordPress article automatically add keywords and keywords seo optimization

Source: Internet
Author: User
Tags wordpress blog
Web site Keywords and Web page description of the search engine friendly network site, if you manually add obviously too much, that how to let WordPress blog automatically for each article automatic keywords and page description. The content of each article is different, how should we let WordPress automatically add article description and keywords? Let's take a look at how to add an article description and keywords to wordpress automatically.
Add the following code to your subject's functions.php file, and the functions of each code are as follows:

Add_action (' Wp_head ', ' wp_keywords '); Add keyword add_action (' wp_head ', ' wp_description '); Add a page description function wp_keywords () {global $s, $post; $keywords = "; if (Is_single ()) {//If it is an article page, the keyword is: Tag + Category ID if (get_th E_tags ($post->id)) {foreach (Get_the_tags ($post->id) as $tag) $keywords. = $tag->name. ', '; } foreach (Get_the_category ($post->id) as $category) $keywords. = $category->cat_name. ', '; $keywords = Substr_replace ($keywords, ',-2); } elseif (Is_home ()) {$keywords = ' I am the homepage keyword ';//home keyword settings} elseif (Is_tag ()) {//tab keyword settings $keywords = single_tag_title ( ", false); } elseif (Is_category ()) {//category page keyword settings $keywords = Single_cat_title ("", false);} elseif (Is_search ()) {//Search page keyword settings $key Words = esc_html ($s, 1); } else {//default page keyword setting $keywords = Trim (Wp_title (", false));} if ($keywords) {//output keyword echo"
 
  
 \ n "; }}function wp_description () {global $s, $post; $description = '; $blog _name = Get_bloginfo (' name '); if (Is_singular ( ) {//Article page if a description field exists, the description is displayed, otherwise the article content is truncated if (! empty ($post->post_excerpt)) {$text = $post->post_excerpt;} else {$te XT = $post->post_content; } $description = Trim (Str_replace ("\ r \ n", "\ r", "\ n", "", ""), "", Str_replace ("\" "," ' ", Strip_ta GS ($text)))); if (!  ($description)) $description = $blog _name. "-" . Trim (Wp_title (", false)); } elseif (Is_home ()) {//First page display description setting $description = $blog _name. "-" . Get_bloginfo (' description '). ' The description of the homepage to be displayed '; Home to add} elseif (Is_tag ()) {//tab page display description Settings $description = $blog _name. "About". Single_tag_title (', false). "' The article"; } elseif (Is_category ()) {//category page displays description settings $description = $blog _name. "About". Single_cat_title (', false). "' The article"; } elseif (Is_archive ()) {//Document page displays description settings $description = $blog _name. "In: '". Trim (Wp_title (", false)). "' The article"; } elseif (Is_search ()) {//Search page displays description settings $description = $blog _name. ": '" . Esc_html ($s, 1). "' Search results"; } else {//default other pages display description settings $description = $blog _name. "About". Trim (Wp_title (", false)). "' The article"; }//Output description $description = mb_substr ($description, 0, $, ' utf-8 '). '..'; echo "
 
  
 \ n ";}

Highlighting keywords in search results:

function Wps_highlight_results ($text) {if (Is_search ()) {$SR = Get_query_var (' s '); $keys = Explode ("", $SR); $text = Preg_ Replace ('/(') Implode (' | ', $keys). /iu ', '. $sr.', $text);} return $text;} Add_filter (' the_excerpt ', ' wps_highlight_results '); Add_filter (' The_title ', ' wps_highlight_results ');

Use this snippet to highlight search terms in your blog search results the_excerpt and the_title.

The above describes the WordPress article automatically add keywords and keywords SEO optimization, 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.