我正在尝试使用一些
preg_replace
从视觉编辑器中删除所有不必要的短代码。例如。有一些这样的元素:
[vc_row][/vc_row][vc_row el_class="hidepdf]
最好的方法是删除
[]
之间的任何内容,从 vc_
开始,然后是任何内容,直到结束括号 ]
我已经尝试使用以下正则表达式:
/\[[\/]?vc_*[^\]]\]/
但是好像不起作用。
尝试这个正则表达式
"/\[(\/*)?vc_(.*?)\]/"
<?php
while($posts->have_posts()) {
$postContent = get_the_content();
//Remove visual composer tags [vc_column] etc
$postContent = preg_replace( "/\[(\/*)?vc_(.*?)\]/", '', $postContent );
}
?>
要从 WordPress 内容中删除不必要的 wpbakery 短代码,您可以在主题的functions.php 文件中使用自定义函数。具体方法如下:
function remove_unnecessary_shortcodes($content) {
$pattern = '/\[\/?vc_[^\]]*\]/';
$replacement = '';
$new_content = preg_replace($pattern, $replacement, $content);
return $new_content;
}
add_filter('the_content', 'remove_unnecessary_shortcodes');
您只需删除管理控制面板中的内容即可。 为什么要更换内容,rofl。
但是如果你非常非常需要:
$start = preg_quote('[vc_row]', '/');
$end = preg_quote('[/vc_row]', '/');
$content = preg_replace("/$start.*$end/imsU", '', $content);