我已经拼凑了下面的代码。我把它保存在一个独立的PHP文件中,这样我就可以按需运行它了。
应该很清楚这意味着什么:循环所有类型为“product”和“product variance”的帖子,然后使用preg\\u replace删除所有div和span标签。
但出于某种原因,以下函数似乎无法删除标记:
<?php
require "wp-config.php";
// Post Types to include
$args = array(
\'posts_per_page\' => \'50\',
\'post_type\' => array(
\'product\',
\'product-variation\'
)
);
// The Query
$query = new WP_Query( $args );
$posts = $query->posts;
foreach($posts as $post) {
$tags = array( \'div\', \'span\');
$content = $post->post_content;
foreach ($tags as $tag) {
$cleanedcontent = preg_replace(\'#<\\s*\' . $tag . \'[^>]*>.*?<\\s*/\\s*\'. $tag . \'>#msi\', \'\', $content);
}
echo $cleanedcontent . \'<hr/>\';
// $cleaned_post = array(
// \'ID\' => $post->ID,
// \'post_content\' => $cleanedcontent
// );
// wp_update_post( $cleaned_post );
}
echo \'Done.\'
?>
最后,我将使用wp\\u update\\u post保存“清理”的内容-该部分当前已被注释掉。我的代码部分基于以下内容:strip only specific tags (like <p>), but keep other tags (like <br/>)
你知道为什么我的函数没有去掉div标签吗?