您可以直接在函数中向内容添加过滤器。php并使用str\\u replace或一些preg\\u replace(regex)来更改内容的输出。未测试。尝试以下操作:
function add_class_to_img_and_p_in_content($content) {
$pattern ="/<img(.*?)class=\\"(.*?)\\"(.*?)>/i";
$replacement = \'<img$1class="$2 newclass newclasstwo"$3>\';
$content = preg_replace($pattern, $replacement, $content);
$pattern ="/<p(.*?)class=\\"(.*?)\\"(.*?)>/i";
$replacement = \'<p$1class="$2 newclass newclasstwo"$3>\';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter(\'the_content\', \'add_class_to_img_and_p_in_content\',11);
如果您的p和img标记还没有class属性,您可以简单地执行以下操作:
function new_content($content) {
$content = str_replace(\'<img\',\'<img class="newImgclass"\', $content);
$content = str_replace(\'<p>\',\'<p class="newPclass">\', $content);
return $content;
}
add_filter(\'the_content\',\'new_content\');