我遇到了WordPress模板的轻微编码问题.这是我在模板中使用的代码:
<?php echo teaser(40); ?>
Run Code Online (Sandbox Code Playgroud)
在我的函数中,我使用它来剥离标记并仅从允许的标记中获取内容.
<?php
function teaser($limit) {
$content = explode(' ', get_the_content(), $limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content).'...';
} else {
$content = implode(" ",$content);
}
$content = preg_replace('/\[.+\]/','', $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
$content = strip_tags($content, '<p><a><ul><li><i><em><strong>');
return $content;
}
?>
Run Code Online (Sandbox Code Playgroud)
问题:我使用上面的代码从内容中删除标签,但WordPress已经将图像标签放在段落中.因此,结果是空段落标记,其中图像被剥离.
只是为了清理我的代码和无用的空标签.我的问题是如何删除空段落标签?
<p></p>
Run Code Online (Sandbox Code Playgroud)
非常感谢提前!:)
php ×1