为了定制特色图片前的段落数,我修改了上述答案,并修改了广告布局解决方案中的代码。
要使用:将以下两个代码段添加到函数中。php和自定义指示的数字,以控制段落数。
// Adds $featured_img and $caption after n paragraph
add_filter( \'the_content\', \'insert_featured_image\' );
function insert_featured_image( $content ) {
if ( has_post_thumbnail($post->ID) ) {
if ( has_post_thumbnail($post->ID) ) {
$caption = get_the_post_thumbnail_caption( $post );
} else {
$caption = \'\';
}
$featured_image = \'<div>\' . get_the_post_thumbnail( $post->ID, \'full\' ) . \'</div>\'.\'<div>\' . $caption . \'</div>\';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $featured_image, 2, $content ); // The numeral here controls the number of pars. Customise it to your needs
}
}
return $content;
}
// Parent function
function prefix_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 );
}