在WordPress内容中为标签添加WordPress分类标签 [英] Add A WordPress Taxonomy Tag for Hashtags in WordPress Content

查看:172
本文介绍了在WordPress内容中为标签添加WordPress分类标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WordPress分类法在SEO中提供帮助,它们使包含大量帖子的网站易于分类.每当帖子保存时,我们如何自动将以#"开头的任何单词转换为WordPress标记.

WordPress taxonomies help in SEO, and they make sites with a large number of posts easy to sort. How can we convert any word starting with ' #' as a WordPress Tag automatically, whenever a post saves.

我的网站上有100篇文章,作者不喜欢放置标签.使用井号标签系统可以确保它们喜欢放置标签.

My site has 100s of posts, and authors Don't like to put tags. Using the system of hashtags can ensure that they enjoy putting tags.

对于每个WordPress帖子保存,我想查找以'#'开头的单词,例如'An #Apple A Day',然后将#Apple转换为默认的WordPress标签.另外,我想针对所有帖子类型执行此操作.

For every WordPress post save, I want to find Words starting with ' #' for example 'An #Apple A Day' and convert #Apple to the default WordPress tag. Also, I want to do it for all post types.

我在注释中找到了一个很好的解释方法,但是由于我不擅长PHP,因此无法使用WordPress帖子的内容.

I found a good explanation of how to do in comments, but because I'm not good at PHP, I couldn't do it with WordPress post's content.

我尝试使用wp_insert_post_data来执行此操作,但是它不起作用.

I tried to do it with wp_insert_post_data, but it doesn't work.

推荐答案

首先,您必须为wordpress构建一个插件,该插件可以链接到已发布的帖子或更新的帖子,您可以参考

first you must to build an plugin for wordpress that hook into the published post or an update post you can refer into this
after that you can add tag whenever they find hastag on post_content and the code goes like this

function post_published_notification( $ID, $post ) {
    $content = $post->post_content;
    preg_match_all('/( #\w+)/', $content, $matches, PREG_PATTERN_ORDER);
    if(isset($matches[1])){
        foreach($matches[1] as $matchKey){
            wp_set_post_tags( $ID, trim($matchKey), true);
        }
    }
}
add_action( 'publish_post', 'post_published_notification', 10, 2 );

如果您使用边境哨所,也许您可​​以使用此

if you use frontier post maybe you can use this

function post_published_from_frontier($my_post){
    $content = $my_post->post_content;
    $ID = $my_post->ID;
    preg_match_all('/( #\w+)/', $content, $matches, PREG_PATTERN_ORDER);
    if(isset($matches[1])){
        foreach($matches[1] as $matchKey){
            wp_set_post_tags( $ID, trim($matchKey), true);
        }
    }
}
add_action('frontier_post_post_save', post_published_from_frontier, 10 ,2 );

您可以参考来更改add_action优先级的参数 并将帖子中的所有hastag更改为url,您可以使用如下代码

you can change the parameter of add_action priority refer to this and to change all of the hastag in the post into url you can use the code like this

function old_wp_content( $content ) { 
    $content =  preg_replace('/ #([A-Za-z0-9\/\.]*)/', ' <a target=\"_blank\" href=\"https://milyin.com/hashtag/$1\">$1</a>', $content);
    return $content;
}
add_filter( 'the_content', 'old_wp_content' ); 

因此,如果我们将所有代码组合到一个插件中,我们可以像这样使用它

so if we combine all of the code into one plugin we can use it like this

<?php
function post_published_notification( $ID, $post ) {
    $content = $post->post_content;
    preg_match_all('/( #\w+)/', $content, $matches, PREG_PATTERN_ORDER);
    if(isset($matches[1])){
        foreach($matches[1] as $matchKey){
            wp_set_post_tags( $ID, trim($matchKey), true);
        }
    }
}
add_action( 'publish_post', 'post_published_notification', 10, 2 );

function post_published_from_frontier($my_post){
    $content = $my_post->post_content;
    $ID = $my_post->ID;
    preg_match_all('/( #\w+)/', $content, $matches, PREG_PATTERN_ORDER);
    if(isset($matches[1])){
        foreach($matches[1] as $matchKey){
            wp_set_post_tags( $ID, trim($matchKey), true);
        }
    }
}
add_action('frontier_post_post_save', post_published_from_frontier, 10 ,2 );

function old_wp_content( $content ) { 
    $content =  preg_replace('/ #([A-Za-z0-9\/\.]*)/', ' <a target=\"_blank\" href=\"https://milyin.com/hashtag/$1\">$1</a>', $content);
    return $content;
}
add_filter( 'the_content', 'old_wp_content' ); 

这篇关于在WordPress内容中为标签添加WordPress分类标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆