我正在寻找一种过滤\\u内容的干净方法,以便在每个H1标记之前添加一段代码。事实上,我有一个很长的隐私政策文本,我想在每个段落之前添加一个返回顶部的链接。当然,我可以轻松地在内容编辑器中添加这些内容,但我不希望我的客户能够意外地将其删除。
所以我有这样的想法:
<h1>BLA BLA BLA</h1>
<p>A very interesting piece of text here...<p>
<h1>BLA BLA BLA</h1>
<p>A very interesting piece of text here...<p>
<h1>BLA BLA BLA</h1>
<p>A very interesting piece of text here...<p>
我想过滤它以获得:
<a href="#">Back to the top</a>
<h1>BLA BLA BLA</h1>
<p>A very interesting piece of text here...<p>
<a href="#">Back to the top</a>
<h1>BLA BLA BLA</h1>
<p>A very interesting piece of text here...<p>
<a href="#">Back to the top</a>
<h1>BLA BLA BLA</h1>
<p>A very interesting piece of text here...<p>
提前感谢您的时间。
干杯
J
最合适的回答,由SO网友:fuxia 整理而成
只是过滤器\'the_content\'
, 测试您是否在correct page, 然后运行str_ireplace()
:
add_filter( \'the_content\', \'wpse_76808_filter_h1\' );
function wpse_76808_filter_h1( $content )
{
// page slug
if ( ! is_page( \'privacy-policy\' ) )
return $content;
return str_ireplace( \'<h1\', \'<a href="#">Back to the top</a><h1\', $content );
}