【问题标题】:Add A WordPress Taxonomy Tag for Hashtags in WordPress Content为 WordPress 内容中的标签添加 WordPress 分类标签
【发布时间】:2020-02-10 05:36:24
【问题描述】:

WordPress 分类法有助于 SEO,它们使包含大量帖子的网站易于排序。每当保存帖子时,我们如何自动将任何以“#”开头的单词转换为 WordPress 标签。

我的网站有 100 多个帖子,作者不喜欢放置标签。使用标签系统可以确保他们喜欢放置标签。

对于每个 WordPress 帖子保存,我想查找以“#”开头的单词,例如“An #Apple A Day”,并将 #Apple 转换为默认的 WordPress 标签。另外,我想对所有帖子类型都这样做。

我在 cmets 中找到了一个很好的解释,但是因为我不擅长 PHP,所以我无法使用 WordPress 帖子的内容来做。

Adding a taxonomy tag to wordpress post by writing it in comment with hashtag

我试图用 wp_insert_post_data 来做,但它不起作用。

【问题讨论】:

    标签: php wordpress plugins tags posts


    【解决方案1】:

    首先,您必须为 wordpress 构建一个插件,该插件可以与已发布的帖子或更新帖子挂钩,您可以参考this
    之后,您可以在他们在 post_content 上找到 hastag 时添加标签,代码如下所示

    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 );
    

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

    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' ); 
    

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

    <?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' ); 
    

    【讨论】:

    • 您好,首先我们不应该使用 save_post 而不是 publish_post 操作,因为作者也可以有私人帖子......其次,我想确保之前必须有一个空格''井号。例如。如果内容是“An#Apple”,那么它不应该将#Apple 转换为 wordpress 标签并将其留空......另外,我可以自动为主题标签创建指向相应分类页面的链接......
    • @AdityaAgarwal 事实是 save_post 和 publish_post 在函数中是相同的。不同的是 publish_post 先运行。对于空间的情况,我已经编辑了我的答案
    • 好吧,先生,很抱歉,代码不起作用。我为了防止冲突禁用了所有其他插件,但我仍然可以确认没有创建标签。 debug.log 显示“为 foreach() 提供的参数无效”
    • 嗨@AdityaAgarwal 我已经编辑了我的代码,对不起我的错,因为我没有从你在帖子中提供的网址检查正则表达式模式。您可以再次尝试我的代码,它可以检测到内容中的主题标签
    • 嘿,再次感谢您的帮助,它运行良好。你能帮我一个小忙,帮我做字符串替换,这样每个标签都可以转换成一个链接...例如#Apple可以链接到milyin.com/hashtag/Apple ...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 2019-12-12
    相关资源
    最近更新 更多