每次给文章加tag都特别头大,现在好了,有大神搞了个代码自动加提取文章中的tag,只要放到function.php中即可。
提交文章的时候,就会自动添加了。
- ///* 自动为文章添加标签 */
- 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 );
- }
- }
- }
代码原理也很简单,来解释一下:
- #表示在提交时执行auto_add_tags方法
- add_action(‘save_post’, ‘auto_add_tags’);
- #获取系统用到的tag
- $tags = get_tags( array(‘hide_empty’ => false) );
- #获取当前post的id
- $post_id = get_the_ID();
- #获取当前post的content
- $post_content = get_post($post_id)->post_content;
- #然后循环tag
- 如果文章中出现了某个tag,就添加这个tag
- foreach ( $tags as $tag ) {
- // 如果文章内容出现了已使用过的标签,自动添加这些标签
- if ( strpos($post_content, $tag->name) !== false)
- wp_set_post_tags( $post_id, $tag->name, true );
- }
同样的思路,应该也可以用到自动添加锚链接上,后面研究一下。
来源于网络