WordPress 自动为新文章添加已使用过的标签

每次都要手动给文章添加标签,很麻烦?不知文章是否出现以前用过的标签,怎么办?以下代码就可以解决这些问题,它会在你发布/保存文章时,检测文章的内容中,是否出现曾经使用过的标签,如果出现,就自动为文章添加这些标签。

auto-add-tags-wpdaxue_com

将代码添加到主题的 functions.php 即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * WordPress 自动为文章添加已使用过的标签
 * https://www.wpdaxue.com/auto-add-tags.html
 */
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 自动为文章添加已使用过的标签
* https://www.wpdaxue.com/auto-add-tags.html
*/
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 );
}
}
}

代码出自:http://wordpress.org/plugins/auto-add-tags/

感谢 @大朗博客 优化增强的版本:

修改后可控制标签输出数量。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 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) {
    $i = 0;
    foreach ( $tags as $tag ) {
    // 如果文章内容出现了已使用过的标签,自动添加这些标签
      if ( strpos($post_content, $tag->name) !== false){
        if ($i == 5) { // 控制输出数量
          break;
        }
        wp_set_post_tags( $post_id, $tag->name, true );
        $i++;
      }
    }
  }
}

// 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) {
$i = 0;
foreach ( $tags as $tag ) {
// 如果文章内容出现了已使用过的标签,自动添加这些标签
if ( strpos($post_content, $tag->name) !== false){
if ($i == 5) { // 控制输出数量
break;
}
wp_set_post_tags( $post_id, $tag->name, true );
$i++;
}
}
}
}

但这样输出,会默认输出前几个标签,考虑到每次输出都是标签库里面的前几个标签,不利于 SEO ,增加了标签打乱功能。

标签打乱增强版:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// WordPress 自动为文章添加已使用过的标签
function array2object($array) { // 数组转对象
  if (is_array($array)) {
    $obj = new StdClass();
    foreach ($array as $key => $val){
      $obj->$key = $val;
    }
  }
  else {
    $obj = $array;
  }
  return $obj;
}
function object2array($object) { // 对象转数组
  if (is_object($object)) {
    foreach ($object as $key => $value) {
      $array[$key] = $value;
    }
  }
  else {
    $array = $object;
  }
  return $array;
}
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) {
    $i = 0;
    $arrs = object2array($tags);shuffle($arrs);$tags = array2object($arrs);// 打乱顺序
    foreach ( $tags as $tag ) {
    // 如果文章内容出现了已使用过的标签,自动添加这些标签
      if ( strpos($post_content, $tag->name) !== false){
        if ($i == 5) { // 控制输出数量
          break;
        }
        wp_set_post_tags( $post_id, $tag->name, true );
        $i++;
      }
    }
  }
}

// WordPress 自动为文章添加已使用过的标签
function array2object($array) { // 数组转对象
if (is_array($array)) {
$obj = new StdClass();
foreach ($array as $key => $val){
$obj->$key = $val;
}
}
else {
$obj = $array;
}
return $obj;
}
function object2array($object) { // 对象转数组
if (is_object($object)) {
foreach ($object as $key => $value) {
$array[$key] = $value;
}
}
else {
$array = $object;
}
return $array;
}
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) {
$i = 0;
$arrs = object2array($tags);shuffle($arrs);$tags = array2object($arrs);// 打乱顺序
foreach ( $tags as $tag ) {
// 如果文章内容出现了已使用过的标签,自动添加这些标签
if ( strpos($post_content, $tag->name) !== false){
if ($i == 5) { // 控制输出数量
break;
}
wp_set_post_tags( $post_id, $tag->name, true );
$i++;
}
}
}
}

@大朗博客:http://www.darlang.com/2018/01/wordpress-automatically-tags-new-posts-controllable-number-of-tags/

本人擅长Ai、Fw、Fl、Br、Ae、Pr、Id、Ps等软件的安装与卸载,精通CSS、JavaScript、PHP、ASP、C、C++、C#、Java、Ruby、Perl、Lisp、Python、Objective-C、ActionScript、Pascal等单词的拼写,熟悉Windows、Linux、OS X、Android、iOS、WP8等系统的开关机。

通过下面的方式来联系我们:

电邮:138762189@qq.com

联系QQ:点击这里给我发消息

官方站:www.tadke.com

※ ※ 联系请加我的企鹅号 ※※

※ ※技术支持请微信联系站长 ※※

Copyright © 2023 Tadke.com. 琼ICP备20000547号