我试图在单篇文章的/h2之后立即插入一个包含少量内容的div。我给了single posts一个新类来帮助触发它,因为我不希望它出现在页面上的/h2之后。我想我需要过滤内容,但不确定如何编写此函数。感谢您的帮助。谢谢
在内容中的%2之后插入div
1 个回复
SO网友:Matthias Lohscheidt
你说得对,你可以用过滤器(the_content
). 我附上了一个例子。你可以把它放到functions.php
你的主题。
function add_content_after_h2($content){
if (is_single()) {
$div = \'<div>small bit of content</div>\';
$content = preg_replace(\'/(<\\/h2>)/i\', \'\\1\'.$div, $content);
}
return $content;
}
add_filter(\'the_content\', \'add_content_after_h2\');
您可以在这里找到Wordpress Codex,其中详细描述了该过滤器:https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content结束