我正在使用以下代码在帖子内容之间显示广告。但是广告却展示了两次。我只希望在第一段末尾放一个广告。
add_filter( 'the_content', 'prefix_insert_inpost_ads' );
function prefix_insert_inpost_ads( $content )
{
$ad_code ='adcode';
if ( is_single() && ! is_admin() )
{
return prefix_insert_after_paragraph( $ad_code, 1, $content );
}
return $content;
}
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content )
{
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
$paragraphAfter=1;
//print_r($paragraphs) ;
//foreach ($paragraphs as $index => $paragraph)
for ($i = 0; $i < count($paragraphs); $i++ )
{
if ( $i == $paragraphAfter )
{
$paragraphs[0] .= $insertion;
}
}
return implode( '', $paragraphs );
}