以下是我在网上找到一些东西后使用的内容(WP初学者)。它找到第四个p标记结束,然后将其添加到后面。最初是在帖子内容中间动态添加广告横幅。
add_filter( \'the_content\', \'prefix_insert_post_related\' );
function prefix_insert_post_related( $content ) {
$related_code .= do_shortcode("[divider]");
$related_code .= do_shortcode("[bws_related_posts]");
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $related_code, 4, $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 );
}