【发布时间】:2019-11-23 18:52:01
【问题描述】:
我正在使用以下代码在页面第 8 段之后插入内容:
add_filter( 'the_content', 'hbg_insert_post_ads_8' );
function hbg_insert_post_ads_8( $content ) {
$ad_code = hbg_ad_code();
if ( is_single() && ! is_admin() ) {
return hbg_insert_after_paragraph( $ad_code, 8, $content );
}
return $content;
}
// Parent Function that makes the magic happen
function hbg_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
但是我想排除某个 div 中的段落,这样内容就不会放在我的突出显示或标注框中。也许不要计算<p> 或将代码放在具有“无广告”类的 div 中。有什么建议吗?
谢谢!
【问题讨论】: