Last update at :2023-12-26,Edit by888u
Thinking that some friends may need such a function, WordPress automatically keywords keywords and description, so I simply found it. If the WordPress you are using now comes with this feature, please ignore it. If not, if you want to try it, I suggest you back up the theme first!
The following code implements using tags as keywords and abstracts as descriptions. If the abstract is not filled in, the first 200 words of the article will be automatically intercepted as descriptions.
Code to implement WordPress automatic keywords and descriptions:
Place the following code before the last ?> of funtions.php in your theme:
//Automatic keywords and description Devework.com function get_cats_name() { $allcats=get_categories(); foreach ($allcats as $category) { $keywords[] = $category->cat_name; } return $keywords; } // utf8 substr function utf8Substr($str, $from, $len) { return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'. '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s', '$1',$str); } // Meta SEO function meta_SEO() { global $post; $output = ''; if (is_single()){//If it is an article page $keywords = ''; $description = ''; if ($post->post_excerpt) {//If the article abstract exists, use the article abstract as the description $description = $post->post_excerpt; $description = str_replace("\r\n","",$description); $description = str_replace("\n","",$description); $description = str_replace("\"","'",$description); $description .= '...'; } else {//If the article abstract does not exist, truncate the first 200 words of the article as a description $description = utf8Substr(strip_tags($post->post_content),0,200); $description = str_replace("\r\n","",$description); $description = str_replace("\n","",$description); $description = str_replace("\"","'",$description); $description .= '...'; } $tags = wp_get_post_tags($post->ID);//Get post tags foreach ($tags as $tag ) { $keywordarray[] = $tag->name; } //Use article tags as keywords $keywords = implode(',',array_unique((array)$keywordarray)); } else {//If it is not an article page $keywords = 'Cheap VPS recommendations, foreign VPS recommendations, free VPS, US VPS discounts, VPS discount codes, how to use Linux VPS, foreign VPS reviews'; //Write the keywords of your blog between the quotation marks, disconnect $description = 'This site recommends excellent American virtual hosts, American VPS hosts, excellent hosts in Hong Kong and foreign vps discounts, host discount codes, free space, virtual hosts, godaddy, namecheap, domain name registration and transfer'; // between quotation marks Write a brief description of your blog, no more than 200 words } //Output keywords $output .= '' . "\n"; $output .= '' . "\n"; //output description echo "$output\n"; } add_action('wp_head', 'meta_SEO');//Add meta_SEO function to the header information
Modify the above content according to your own site, that is, the content on lines 43 and 44 needs to be modified according to your website.
Recommended site searches: PHP space rental, ICP registration query, free vps trial 7-day accelerator, website registration number query, application for free space and domain name, latest domain name query, server rental, domain name agency, virtual space, ICP registration, < /p>
发表评论