: This article mainly introduces the SEO optimization of automatically adding keywords and keywords in WordPress articles. if you are interested in PHP tutorials, please refer to it. The keyword and description of a website are related to the friendliness of a website to search engines. if you manually add a keyword, how can you enable the wordpress blog to automatically describe the keyword and webpage of each article. The content of each article is different. how can we enable wordpress to automatically add the article description and keywords? Next let's take a look at how to automatically add the article description and keywords to wordpress.
Add the following code to the functions. php file of your topic. the function of each code is parsed as follows:
Add_action ('WP _ head', 'WP _ keyword'); // add the keyword add_action ('WP _ head', 'WP _ description '); // add the page description function wp_keywords () {global $ s, $ post; $ keywords = ''; if (is_single () {// if it is an article page, keywords: tag + Category ID if (get_the_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 = 'My homepage keywords '; // homepage keyword settings} elseif (is_tag () {// tab keyword settings $ keywords = single_tag_title ('', false);} elseif (is_category ()) {// keyword settings on the category page $ keywords = single_cat_title ('', false);} elseif (is_search () {// keyword settings on the search page $ keywords = esc_html ($ s, 1);} else {// keyword settings on the response page $ 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 ()) {// if the description field exists on the article page, the description is displayed. Otherwise, the content of the article if (! Empty ($ post-> post_excerpt) {$ text = $ post-> post_excerpt;} else {$ text = $ post-> post_content ;} $ description = trim (str_replace (array ("\ r \ n", "\ r", "\ n ","",""),"", str_replace ("\" "," '", strip_tags ($ text); if (! ($ Description) $ description = $ blog_name. "-". trim (wp_title ('', false);} elseif (is_home () {// Set the homepage display description $ description = $ blog_name. "-". get_bloginfo ('Description '). 'Description to be displayed on the homepage '; // add elseif (is_tag () {// Set the description on the TAB $ description = $ blog_name. "Related '". single_tag_title ('', false ). "'article";} elseif (is_category () {// The description settings are displayed on the category page $ description = $ blog_name. "Related '". single_cat_title ('', false ). "'article";} elseif (is_archive () {// The description settings are displayed on the document page $ description = $ blog_name. "In :'". trim (wp_title ('', false )). "'article";} elseif (is_search () {// Set the description displayed on the search page $ description = $ blog_name. ":'". esc_html ($ s, 1 ). "'Search result";} else {// by default, other pages show the description settings $ description = $ blog_name. "Related '". trim (wp_title ('', false )). "'article";} // output description $ description = mb_substr ($ description, 0,220, 'utf-8 '). '.. '; echo"
\ N ";}
Highlight 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 code snippet to highlight the search results the_excerpt and the_title in your blog.
The above introduces the SEO optimization for automatically adding keywords and keywords in WordPress articles, including some content, and hope to help those who are interested in PHP tutorials.