Last update at :2023-12-26,Edit by888u
Wordpress is a good website building program. Whether it is a business or an individual user, there are still a lot of users. Especially in terms of blog website building, it can be said that the user group is very large. There are some aspects of SEO optimization that you may need to do yourself, such as automatically adding alt and title attributes to article images, and automatically adding internal links to article tags. Of course, there are many WordPress themes that integrate such functions, and there are also many WordPress themes that do not add them. However, you can add them yourself. The specific operation method is relatively simple. You only need to copy the code to the theme function functions.php In the file, the function can be realized.
1. Add the following code to the functions.php file to automatically add alt and title attributes to article images:
//Article pictures automatically add alt and title attributes
function image_alt_tag($content){
global $post;preg_match_all('/
/', $content, $images);
if(!is_null($images)) {foreach($images[1] as $index => $value)
{$new_img = str_replace('!['.get_the_title().'-'.get_bloginfo ('name').' '.get_the_title().'-'.get_bloginfo('name').']()
2. Add the following code to the functions.php file with a theme to automatically add internal links to the tags of the article:
/* Automatically add internal links to tags in articles */ $match_num_from = 1; //If the same tag is used less than several times in an article, it will not be automatically linked. $match_num_to = 1; //The maximum number of times the same tag can be automatically linked in an article function tag_sort($a, $b){ if ( $a->name == $b->name ) return 0; return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1; } function tag_link($content){ global $match_num_from,$match_num_to; $posttags = get_the_tags(); if ($posttags) { usort($posttags, "tag_sort"); foreach($posttags as $tag) { $link = get_tag_link($tag->term_id); $keyword = $tag->name; $cleankeyword = stripslashes($keyword); $url = ""; $limit = rand($match_num_from,$match_num_to); $content = preg_replace( '|(]+>)(.*)('.$ex_word.')(.*)(]*>)|U'.$case , '$1$2%&&&&&%$4$5', $content); $content = preg_replace( '|()|U'.$case, '$1$2%&&&&&%$4$5', $ content); $cleankeyword = preg_quote($cleankeyword,'\''); $regEx = '\'(?!((<.*?)|(
]*?)>)| ([^>]*?))\'s' . $case; $content = preg_replace($regEx,$url,$content,$limit); $content = str_replace( '%&&&&&%', stripslashes($ex_word), $content); } } return $content; } add_filter('the_content','tag_link',1); /* Automatically add internal links to end tags in articles */
Note: The above code comes from the Internet, and the specific originality is unknown; in addition, please be sure to back up the original file when adding it, just in case you need it! In addition, the code may be invalid or incorrect for some themes, so there is no guarantee that it will work for all themes!
Recommended site searches: domain name registration query, registration-free space in mainland China, foreign servers, free personal website servers, Ministry of Industry and Information Technology registration URLs, overseas servers, registration numbers, foreign free all-purpose space, multi-IP station group servers, Hong Kong free virtual hosting ,
发表评论