我需要写在Wp数据库(内部的帖子)一个锚文本链接给定的关键字在100多个帖子在一个特定的时间段内(发布的帖子在16-01-2013和23-05-2013之间)。
我考虑过使用一个插件,但除了日期范围不可能,所有的链接必须留在帖子一旦插件被禁用…
我也想使用sql查询,但它会在图像上写链接,我不知道如何设置一个时间范围
UPDATE wp_posts SET post_content = REPLACE ( post_content, 'my keyword', '<a href="#">my keyword</a>' );
有简单的方法来做这个还是我必须手工做?
UPDATE wp_posts SET post_content = REPLACE ( post_content, 'my keyword', '<a href="#">my keyword</a>' ) WHERE <insert date field> >= 2013-01-16 AND <insert date field> <= '2013-05-23';
您可以使用post_date_gmt或post_date字段。下面的示例使用gmt日期
UPDATE wp_posts SET post_content = REPLACE ( post_content, 'my keyword', '<a href="#">my keyword</a>' ) WHERE post_date_gmt >= '2013-01-16 00:00:00' and post_date_gmt <= '2013-05-23 23:59:59'
来防止编辑图像标题就比较复杂了。我想我会为时间空间中的所有内容字段做一个foreach,并使用正则表达式找出要替换的内容。
我发现了另一个关于这个问题的帖子:http://www.marcogoncalves.com/2011/03/php-regexp-replace-words-in-html-string-if-not-inside-tags/
好吧,这不是解决问题的方法,但是不管怎样!
如果你担心链接被破坏,那么在你运行update关键字查询之前,只需运行其他几个查询,如
UPDATE wp_posts SET post_content = REPLACE ( post_content, 'title="my keyword"', 'title="Xmy keywordX"'
UPDATE wp_posts SET post_content = REPLACE ( post_content, 'alt="my keyword"', 'alt="Xmy keywordX"'
这样链接就不会被搞砸了。
然后运行更新查询
UPDATE wp_posts SET post_content = REPLACE ( post_content, 'my keyword', '<a href="#">my keyword</a>' )
然后重新设置标题和alt。
UPDATE wp_posts SET post_content = REPLACE ( post_content, 'title="Xmy keywordX"', 'title="my keyword"'
UPDATE wp_posts SET post_content = REPLACE ( post_content, 'alt="Xmy keywordX"', 'alt="my keyword"'
就像我说的,这不是最好的方法。
我会查询所有的记录,然后循环遍历它们,并用正则表达式替换我想要的东西,以类似Steven发布的链接的方式。