SEO Optimization for automatically adding keywords and keywords in WordPress articles, wordpressseo
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-> c At_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 detail page $ keywords = trim (wp_title ('', False);} if ($ keywords) {// output keyword echo "<meta name = \" keywords \ "content = \" $ keywords \ "/> \ n" ;}} function wp_description () {global $ s, $ post; $ description = ''; $ blog_name = get_bloginfo ('name'); if (is_singular () {// if a description field exists on the article page, the description is displayed. Otherwise, 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 "<meta name = \" description \ "content = \" $ description \ "/>\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', '<strong>'.$sr.'</strong>', $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.