下面是我用来在帖子第二段之后添加小部件的函数。这只适用于第一篇文章和单个文章,因此如果需要在所有文章中显示这一点,则需要对其进行一些调整。您应该能够修改此选项以满足您的需要。希望这有帮助
// Add advertising widget within the firts post and single posts
function pietergoosen_insert_content_after_second_paragraph_filter( $content ) {
static $first;
$first = (!isset($first)) ? true : false;
if (true == $first) {
ob_start();
echo \'<div class="widget-box">\';
dynamic_sidebar( \'sidebar-19\' );
echo \'</div><!-- end .widget-box -->\';
$new_content = ob_get_clean();
if ( ! is_admin() ) {
return pietergoosen_insert_content_after_second_paragraph( $new_content, 2, $content );
}
}
return $content;
}
add_filter(\'the_content\',\'pietergoosen_insert_content_after_second_paragraph_filter\');
// Paragraph explode and insert
function pietergoosen_insert_content_after_second_paragraph( $new_content, $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] .= $new_content;
}
}
return implode( \'\', $paragraphs );
}