WordPress-如何在get_the_content()的第一段之后添加内容

时间:2019-02-11 10:18:46

标签: php wordpress

有人可以告诉我如何更改代码以与get_the_content()函数一起使用吗?我试图将“ the_content”更改为“ get_the_content”,但无法正常工作。

//Insert ads after first paragraph of single post content.
add_filter( 'the_content', 'prefix_insert_post_ads' );
     function prefix_insert_post_ads( $content ) {

 $ad_code = '<div>Ads code goes here</div>';

 if ( is_single() && ! is_admin() ) {
  return prefix_insert_after_paragraph( $ad_code, 1, $content );
 }

 return $content;
}

// Parent Function that makes the magic happen
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 );
}

0 个答案:

没有答案