听说有人在找wordpress插入图片的代码?
把我插入广告的代码改改,应该能用
- add_filter( 'the_content', 'prefix_insert_post_img' );function prefix_insert_post_img($content){$pattern = '/<p>.*?<\/p>/';preg_match_all($pattern,$content,$jg); //计算文章的段落数量$paragraph_count=count($jg[0]);if($paragraph_count >= 2 && is_single()){//如果文章的段落数量少于2段,则不会插入文章段落图片$insert_paragraph=1;//插入段落,自己修改$img_code = '<p></p>';return prefix_insert_after_paragraph( $img_code, $insert_paragraph, $content );}return $content;}// 插入图片所需的功能代码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 );}