精文章自动内链和自动添加标签

综合1 5,271,945字数 506阅读1分41秒

WordPress文章自动添加标签的代码

它的用途是在你写完这篇文章发布的时候,自动检测你这篇文章中有的关键字,然后把这个关键字转成标签给你添加进去,这个貌似需要在后台发布文章才可以生效,也可以在前台编辑完文章发布,然后到后台再发布一遍文章源自小武站https://1z345.cn/小武站-https://bbs.50-0.cn/997.html

复制代码
  1. add_action('save_post', 'auto_add_tags');
  2. function auto_add_tags()
  3. {
  4. $tags = get_tags(array('hide_empty' => false));
  5. $post_id = get_the_ID();
  6. $post_content = get_post($post_id)->post_content;
  7. if ($tags) {
  8. foreach ($tags as $tag) {
  9. // 如果文章内容出现了已使用过的标签,自动添加这些标签
  10. if (strpos($post_content, $tag->name) !== false) {
  11. wp_set_post_tags($post_id, $tag->name, true);
  12. }
  13. }
  14. }
  15. }

二、Wordpress自动为标签添加内链的代码:文章源自小武站https://1z345.cn/小武站-https://bbs.50-0.cn/997.html

它的用途就是你在写这篇文章的时候,如果添加了标签,它会自动把文章内含有标签的字添加成内链文章源自小武站https://1z345.cn/小武站-https://bbs.50-0.cn/997.html

复制代码
  1. //WordPress文章关键词自动内链 大白博客
  2. function tag_sort($a, $b){
  3. if ( $a->name == $b->name ) return 0;
  4. return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;
  5. }
  6. function tag_link($content){
  7. $match_num_from = 1; //一个标签少于几次不链接
  8. $match_num_to = 1; //一个标签最多链接几次
  9. $posttags = get_the_tags();
  10. if ($posttags) {
  11. usort($posttags, "tag_sort");
  12. foreach($posttags as $tag) {
  13. $link = get_tag_link($tag->term_id);
  14. $keyword = $tag->name;
  15. //链接代码
  16. $cleankeyword = stripslashes($keyword);
  17. $url = "<a href=\"$link\" title=\"".str_replace('%s',addcslashes($cleankeyword, '$'),__('更多关于 %s 的文章-大白博客'))."\"";
  18. $url .= ' target="_blank"';
  19. $url .= ">".addcslashes($cleankeyword, '$')."</a>";
  20. $limit = rand($match_num_from,$match_num_to);
  21. //不链接代码
  22. $content = preg_replace( '|(<a[^>]+>)(.*)<pre.*?>('.$ex_word.')(.*)<\/pre>(</a[^>]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
  23. $content = preg_replace( '|(<img)(.*?)('.$ex_word.')(.*?)(>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
  24. $cleankeyword = preg_quote($cleankeyword,'\'');
  25. $regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s' . $case;
  26. $content = preg_replace($regEx,$url,$content,$limit);
  27. $content = str_replace( '%&&&&&%', stripslashes($ex_word), $content);
  28. }
  29. }
  30. return $content;
  31. }
  32. add_filter('the_content','tag_link',1);

 文章源自小武站https://1z345.cn/小武站-https://bbs.50-0.cn/997.html 文章源自小武站https://1z345.cn/小武站-https://bbs.50-0.cn/997.html

继续阅读
给导航菜单添加自定义徽章及多种样式菜单设置方法
给导航菜单添加自定义徽章及多种样式菜单设置方法 综合

给导航菜单添加自定义徽章及多种样式菜单设置方法

子比主题的菜单显示其实是支持自定义Html代码的,那么就有很多的扩展性了!之前我们详细的讲解过如何在导航菜单添加图标的教程,如果不会添加图标的朋友可以复习一下下方文章!本篇文章属于扩展功能,高级教程。...
  • 本文由 admin 发表于2024年8月23日 12:24:28
  • 转载请务必保留本文链接:https://bbs.50-0.cn/997.html
    • 小唐
      小唐

      文章自动添加标签的代码

    匿名

    发表评论

    匿名网友
    确定