1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > WordPress 自动为文章添加标签内链 文章自动添加标签

WordPress 自动为文章添加标签内链 文章自动添加标签

时间:2021-01-16 17:14:27

相关推荐

WordPress 自动为文章添加标签内链 文章自动添加标签

WordPres标签是一个好功能,善用标签不但可以加强文章间的关联性提升用户体验,也可以借助标签加强站内SEO内链。

以下内容使用方法: 将代码添加至主题functions.php文章中即可。

WordPress文章自动添加标签:

/* 自动为文章添加标签 */add_action('save_post', 'xy_add_tags');function xy_add_tags(){$tags = get_tags( array('hide_empty' => false) );$post_id = get_the_ID();$post_content = get_post($post_id)->post_content;if ($tags) {foreach ( $tags as $tag ) {// 如果文章内容出现了已使用过的标签,自动添加这些标签if ( strpos($post_content, $tag->name) !== false)wp_set_post_tags( $post_id, $tag->name, true );}}}

WordPress 标签自动添加内链:

/* 自动为文章内的标签添加内链 */function xy_add_tag_link($content){$limit = 1; // 设置同一个标签添加几次链接$posttags = get_the_tags();if ($posttags) {foreach($posttags as $tag) {$link = get_tag_link($tag->term_id);$keyword = $tag->name;$cleankeyword = stripslashes($keyword);$url = '<a target="_blank" href="'.$link.'" title="'.str_replace('%s', addcslashes($cleankeyword, '$'), __('View all posts in %s')).'">'.addcslashes($cleankeyword, '$').'</a>';$regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s';$content = preg_replace($regEx,$url,$content,$limit);}}return $content;}add_filter( 'the_content', 'xy_add_tag_link', 1 );

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。