北海的鱼 - 美化 2023-07-20T14:39:00+08:00 Typecho https://blog.qsuu.cn/feed/atom/tag/%E7%BE%8E%E5%8C%96/ <![CDATA[文章自动内链和自动添加标签]]> https://blog.qsuu.cn/archives/41.html 2023-07-20T14:39:00+08:00 2023-07-20T14:39:00+08:00 北海的鱼 https://blog.qsuu.cn 教程开始

WordPress网站做SEO优化时,内链是一个很重要的因素,而我们平时都是手动为文章添加内链,这样会牺牲一些时间,其实我们可以让网站自动为文章生成标签,并且为标签自动添加内链。以前有一款插件 WP Keyword Link ,但是刚才没搜索到,其实我们还可以使用纯代码实现这个功能。

本文所有内容均添加到子比目录下functions.php文件里面

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

它的用途是在你写完这篇文章发布的时候,自动检测你这篇文章中有的关键字,然后把这个关键字转成标签给你添加进去,这个貌似需要在后台发布文章才可以生效,也可以在前台编辑完文章发布,然后到后台再发布一遍。

add_action('save_post', 'auto_add_tags');
function auto_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自动为标签添加内链的代码

它的用途就是你在写这篇文章的时候,如果添加了标签,它会自动把文章内含有标签的字添加成内链。

//WordPress文章关键词自动内链 大白博客
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){
  $match_num_from = 1;  //一个标签少于几次不链接
  $match_num_to = 1;  //一个标签最多链接几次
  $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 = "<a href=\"$link\" title=\"".str_replace('%s',addcslashes($cleankeyword, '$'),__('更多关于 %s 的文章-大白博客'))."\"";
      $url .= ' target="_blank"';
      $url .= ">".addcslashes($cleankeyword, '$')."</a>";
      $limit = rand($match_num_from,$match_num_to);
      //不链接代码
      $content = preg_replace( '|(<a[^>]+>)(.*)<pre.*?>('.$ex_word.')(.*)<\/pre>(</a[^>]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
      $content = preg_replace( '|(<img)(.*?)('.$ex_word.')(.*?)(>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
      $cleankeyword = preg_quote($cleankeyword,'\'');
      $regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'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);

直接放主题目录下functions.php文件里面。

]]>
<![CDATA[zibll主题给你的首页侧边栏增加10条或者更多轮播广告位 文字版+图片版]]> https://blog.qsuu.cn/archives/32.html 2023-03-16T16:21:50+08:00 2023-03-16T16:21:50+08:00 北海的鱼 https://blog.qsuu.cn 文字版轮播+图片轮播
时间可以自己调整
文字版代码

[acc status="close" title="代码"]

<style>
  .advertise-container {
    width: 100%;
    height: 100px;
    overflow: hidden;
    position: relative;
    text-align: center;
  }
  .advertise-container .advertise-item {
    width: 100%;
    height: 100px;
    position: absolute;
    top: 0;
    left: 0;
  }
  .advertise-container a {
    display: inline-block;
    width: 100%;
    height: 100%;
    line-height: 100px;
    color: #000;
    font-weight: bold;
    text-decoration: none;
    font-size: 24px;
    animation: pulse 2s ease infinite;
    background: transparent;
  }
  @keyframes pulse {
    0% {
      color: #000;
    }
    50% {
      color: #f00;
    }
    100% {
      color: #000;
    }
  }
</style>

<div class="advertise-container">
  <a href="https://www.baidu.com" target="_blank">
    <div class="advertise-item">广告位1</div>
  </a>
  <a href="https://www.baidu.com" target="_blank">
    <div class="advertise-item">广告位2</div>
  </a>
  <a href="https://www.baidu.com" target="_blank">
    <div class="advertise-item">广告位3</div>
  </a>
  <a href="https://www.baidu.com" target="_blank">
    <div class="advertise-item">广告位4</div>
  </a>
  <a href="https://www.baidu.com" target="_blank">
    <div class="advertise-item">广告位5</div>
  </a>
  <a href="https://www.baidu.com" target="_blank">
    <div class="advertise-item">广告位6</div>
  </a>
  <a href="https://www.baidu.com" target="_blank">
    <div class="advertise-item">广告位7</div>
  </a>
  <a href="https://www.baidu.com" target="_blank">
    <div class="advertise-item">广告位8</div>
  </a>
  <a href="https://www.baidu.com" target="_blank">
    <div class="advertise-item">广告位9</div>
  </a>
  <a href="https://www.baidu.com" target="_blank">
    <div class="advertise-item">广告位10</div>
  </a>
</div>

<script>
  var advertise = document.querySelectorAll('.advertise-item');
  var index = 0;
  setInterval(function() {
    advertise.forEach(function(item) {
      item.style.display = 'none';
    })
    if (index >= advertise.length) {
      index = 0;
    }
    advertise[index].style.display = 'block';
    index++;
  }, 2000)
</script>

[/acc]
图片版

[acc status="close" title="代码"]

<style>
  .advertise-container {
    width: 100%;
    height: 100px;
    overflow: hidden;
    position: relative;
  }
  .advertise-container .advertise-item {
    width: 100%;
    height: 100px;
    position: absolute;
    top: 0;
    left: 0;
    object-fit: cover;
  }
  .advertise-container a {
    display: inline-block;
    width: 100%;
    height: 100%;
    line-height: 100px;
    color: #000;
    font-weight: bold;
    text-decoration: none;
    font-size: 24px;
    animation: pulse 2s ease infinite;
    background: transparent;
    text-align: center;
  }
  .advertise-container img {
    width: 100%;
    height: 100%;
  }
  @keyframes pulse {
    0% {
      color: #000;
    }
    50% {
      color: #f00;
    }
    100% {
      color: #000;
    }
  }
</style>

<div class="advertise-container">
  <a href="https://www.baidu.com" target="_blank">
    <div class="advertise-item"><img src="https://www.baidu.com/img/baidu_jgylogo3.gif" alt="广告位1"></div>
  </a>
  <a href="https://www.baidu.com" target="_blank">
    <div class="advertise-item"><img src="图片链接" alt="广告位2"></div>
  </a>
  <a href="https://www.baidu.com" target="_blank">
    <div class="advertise-item"><img src="图片链接" alt="广告位3"></div>
  </a>
  <a href="https://www.baidu.com" target="_blank">
    <div class="advertise-item"><img src="图片链接" alt="广告位4"></div>
  </a>
  <a href="https://www.baidu.com" target="_blank">
    <div class="advertise-item"><img src="https://www.baidu.com/img/baidu_jgylogo3.gif" alt="广告位5"></div>
  </a>
  <a href="https://www.baidu.com" target="_blank">
    <div class="advertise-item"><img src="https://www.baidu.com/img/baidu_jgylogo3.gif" alt="广告位6"></div>
  </a>
  <a href="https://www.baidu.com" target="_blank">
    <div class="advertise-item"><img src="https://www.baidu.com/img/baidu_jgylogo3.gif" alt="广告位7"></div>
  </a>
  <a href="https://www.baidu.com" target="_blank">
    <div class="advertise-item"><img src="https://www.baidu.com/img/baidu_jgylogo3.gif" alt="广告位8"></div>
  </a>
  <a href="https://www.baidu.com" target="_blank">
    <div class="advertise-item"><img src="https://www.baidu.com/img/baidu_jgylogo3.gif" alt="广告位9"></div>
  </a>
  <a href="https://www.baidu.com" target="_blank">
    <div class="advertise-item"><img src="https://www.baidu.com/img/baidu_jgylogo3.gif" alt="广告位10"></div>
  </a>
</div>

<script>
  var advertise = document.querySelectorAll('.advertise-item');
  var index = 0;
  setInterval(function() {
    advertise.forEach(function(item) {
      item.style.display = 'none';
    })
    if (index >= advertise.length) {
      index = 0;
    }
    advertise[index].style.display = 'block';
    index++;
  }, 2000)
</script>

[/acc]
放在任意侧边栏即可

]]>
<![CDATA[给Cuteen主题安排上侧栏标签云]]> https://blog.qsuu.cn/archives/25.html 2023-03-15T09:49:13+08:00 2023-03-15T09:49:13+08:00 北海的鱼 https://blog.qsuu.cn 借鉴了小何博客的代码,自己试了下发现不对称就改了一个数值。每次刷新颜色都会改变,目前有8组颜色,具体效果看我的侧边栏。

引入自定义CSS

/*侧栏标签云*/
.flink-item{width:63px;height: 25px; margin:3px; background-color:#57b027; border-radius:2px; text-align: center;}
.flink-title a{ font-size:10px; color:#ffffff; margin:3px; }
.flink-title a:hover{ color:#cccccc; }
.bg-1{background-color:#b57dff!important}
.bg-2{background-color:#465efb!important}
.bg-3{background-color:#f9bb3c!important}
.bg-4{background-color:#f55555!important}
.bg-5{background-color:#f6969a!important}
.bg-6{background-color:#e92b6f!important}
.bg-7{background-color:#8c76f!important}
.bg-8{background-color:#3fa5ff!important}

通过函数调用标签

路径 /Cuteen/include/sidebar.php 合适位置加入下方代码
北海的鱼

<!-- 标签云 -->
<section class="sidebar-comment-box card mt-4">
<div class="px-3 py-2 my-2 d-flex align-items-center border-bottom">
<svg t="1678186881052" class="icon icon-20 me-1" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="13180" width="20" height="20"><path d="M511.744 514.7136m-450.816 0a450.816 450.816 0 1 0 901.632 0 450.816 450.816 0 1 0-901.632 0Z" fill="#1296db" p-id="13181" data-spm-anchor-id="a313x.7781069.0.i20" class="selected"></path><path d="M405.0432 356.4544l154.9824-143.4624c24.9344-23.0912 65.3312-23.296 90.5728-0.4608l100.5056 91.0336c14.9504 13.5168 15.6672 35.584 1.6384 49.9712l-347.6992 2.9184zM293.376 356.1472l94.976-142.0288c16.7936-26.7264 52.3776-37.8368 83.2512-26.0096 23.2448 8.9088 45.0048 17.3056 44.3392 17.3056-1.4336 0-163.6352 150.016-163.6352 150.016l-58.9312 0.7168zM749.6192 402.176H280.1664c-44.9536 0-81.4592 36.4544-81.4592 81.4592v213.0944c0 44.9536 36.4544 81.4592 81.4592 81.4592h469.4528c44.9536 0 81.4592-36.4544 81.4592-81.4592V483.584c-0.0512-44.9536-36.5056-81.408-81.4592-81.408z m-430.7968 237.7216c-27.4944 0-49.7664-22.272-49.7664-49.7664s22.272-49.7664 49.7664-49.7664c27.4944 0 49.7664 22.272 49.7664 49.7664s-22.272 49.7664-49.7664 49.7664z" fill="#FFFFFF" p-id="13182"></path></svg>
  <span>标签云</span> <span class="ios"></span> </div>
<div class="px-2 pb-3">

<ul class="flinks"><li></li><li></li><li></li><li></li></ul>
<?php $this->widget('Widget_Metas_Tag_Cloud', 'ignoreZeroCount=1&limit=28')->to($tags); ?>
<?php while($tags->next()): ?>
<script>document.querySelectorAll('ul.flinks').forEach(function(e){let a=e;if(a){let ns=a.querySelectorAll("li");let str='<div style="display:inline-block;; float:left">';let bgid=0;const bgs=["bg-1","bg-2","bg-3","bg-4","bg-5","bg-6","bg-7","bg-8"];for(let i=0;i<ns.length;i+=4){str+=(`<div class="flink-item ${bgs[Math.floor(Math.random() * 8)]}"><div class="flink-title"><a href="<?php $tags->permalink(); ?>"><?php $tags->name(); ?></a></div></div>`)}str+=`</div>`;let n1=document.createElement("div");n1.innerHTML=str;a.parentNode.insertBefore(n1,a);a.style="display: none;"}else{console.log('No such id "flinks"')}});</script>
<?php endwhile; ?>

</div></section>
<!-- 标签云 -->

另外我改的数值是以下地方
北海的鱼
原本是3 我改成了2 如果你们不对称,也可以修改试试。

]]>