我的方法(see update below):
function addParagraphs($content) {
// you can add as many as you want:
$additions = array(
\'<p>After 1st paragraph</p>\',
\'<p>After 2nd paragraph</p>\'
);
$content = get_the_content();
$output = \'\'; // define variable to avoid PHP warnings
$parts = explode("</p>", $content);
$count = count($parts); // call count() only once, it\'s faster
for($i=0; $i<$count; $i++) {
$output .= $parts[$i] . \'</p>\' . $additions[$i]; // non-existent additions does not concatenate
}
return $output;
}
add_filter(\'the_content\',\'addParagraphs\');
Answer is updated according to subsequent comments:
$paragraphAfter[1] = \'<div>AFTER FIRST</div>\'; //display after the first paragraph
$paragraphAfter[3] = \'<div>AFTER THIRD</div>\'; //display after the third paragraph
$paragraphAfter[5] = \'<div>AFTER FIFtH</div>\'; //display after the fifth paragraph
$content = apply_filters( \'the_content\', get_the_content() );
$content = explode("</p>", $content);
$count = count($content);
for ($i = 0; $i < $count; $i++ ) {
if ( array_key_exists($i, $paragraphAfter) ) {
echo $paragraphAfter[$i];
}
echo $content[$i] . "</p>";
}