如何在特定类别中查找和替换(或添加)特定单词?

时间:2017-02-16 作者:Yearmaz

我有一个网站,所有帖子都有下载链接。下载链接如下:http://sitename.com/FileID/filename.zip

我想在每个类别的文件名之前添加不同的单词。例如:

对于类别,我想在文件名之前添加一个类别。新链接如下所示:http://sitename.com/FileID/ACategory-filename.zip

对于B类别,我想在filename之前添加BCategory。新链接如下所示:http://sitename.com/FileID/BCategory-filename.zip

我该怎么做?在phpmyadmin中有没有插件或者有什么方法可以做到这一点?

编辑:

我发现此代码仅在我选择的类别中查找和替换:

UPDATE wp_term_relationships r JOIN wp_posts p 
ON r.object_id = p.id JOIN wp_terms t
ON r.term_taxonomy_id = t.term_id JOIN wp_term_taxonomy x
ON r.term_taxonomy_id = x.term_taxonomy_id
SET p.post_content = REPLACE(post_content, \'Item to replace here\', \'Replacement text here\')
WHERE t.name = \'CATEGORY \'
AND x.taxonomy = \'category\' 
现在我找不到任何改变的方法http://sitename.com/FileID/filename.zip to http://sitename.com/FileID/ACategory-filename.zip

1 个回复
SO网友:CodeMascot

经过长时间的尝试才找到解决方案-

UPDATE wp_posts p 
       JOIN wp_term_relationships r 
         ON r.object_id = p.id 
       JOIN wp_terms t 
         ON r.term_taxonomy_id = t.term_id 
       JOIN wp_term_taxonomy x 
         ON r.term_taxonomy_id = x.term_taxonomy_id 
SET    p.post_content = CASE
    WHEN t.name = \'ACategory\' THEN REPLACE(post_content, \'http://sitename.com/FileID/filename.zip\', 
                               \'http://sitename.com/FileID/ACategory-filename.zip\')
    WHEN t.name = \'BCategory\' THEN REPLACE(post_content, \'http://sitename.com/FileID/filename.zip\', 
                               \'http://sitename.com/FileID/BCategory-filename.zip\')
    ELSE post_content
    END
WHERE x.taxonomy = \'category\'
仔细看,我对您的代码做了一些更改。并特别注意CASE 我在代码中包含的命令。为每个类别名称添加一个额外的WHEN t.name = \'Your-Category-Name\' THEN REPLACE(post_content, \'http://sitename.com/FileID/filename.zip\', \'http://sitename.com/FileID/new-filename.zip\').

希望这有帮助。

I tested it and it worked perfectly in my system.

相关推荐

Admin Theme customization

我遵循wordpress codex网站上关于通过插件创建管理主题的说明。我激活了插件,但我的样式表没有包含在<head>.. 这是我的代码:add_action( \'admin_init\', \'kd_plugin_admin_init\' ); add_action( \'admin_menu\', \'kd_plugin_admin_menu\' ); function kd_plugin_admin_init() { /* Register