admin管理员组文章数量:1130349
I am so close I can feel it. I am trying to get an ad block to show up after say 2 paragraphs. Currently I am using the following code in my functions.php to put the adblock before the last paragraph.
I cannot for the life of me find the correct code to get this done.
function ads_added_above_last_p($text) {
if( is_single() ) :
$ads_text = '<div class="wpselect_middle_content">My Ad Code Here</div>';
if($pos1 = strrpos($text, '<p>')){
$text1 = substr($text, 0, $pos1);
$text2 = substr($text, $pos1);
$text = $text1 . $ads_text . $text2;
}
endif;
return $text;
}
add_filter('the_content', 'ads_added_above_last_p');
If I play with the second $text string and put $pos2 it works perfectly BUT it duplicates all of the text within the post.
Any help would be greatly appreciated.
I am so close I can feel it. I am trying to get an ad block to show up after say 2 paragraphs. Currently I am using the following code in my functions.php to put the adblock before the last paragraph.
I cannot for the life of me find the correct code to get this done.
function ads_added_above_last_p($text) {
if( is_single() ) :
$ads_text = '<div class="wpselect_middle_content">My Ad Code Here</div>';
if($pos1 = strrpos($text, '<p>')){
$text1 = substr($text, 0, $pos1);
$text2 = substr($text, $pos1);
$text = $text1 . $ads_text . $text2;
}
endif;
return $text;
}
add_filter('the_content', 'ads_added_above_last_p');
If I play with the second $text string and put $pos2 it works perfectly BUT it duplicates all of the text within the post.
Any help would be greatly appreciated.
Share Improve this question asked Aug 31, 2013 at 1:47 Matthew SniderMatthew Snider 372 silver badges7 bronze badges1 Answer
Reset to default 3I've found explode() to be useful when trying to break strings apart. This code creates an array of paragraph chunks, inserts the new block after two paragraphs and concatenates it back into a string for output.
function insert_ad_block( $text ) {
if ( is_single() ) :
$ads_text = '<div class="wpselect_middle_content">My Ad Code Here</div>';
$split_by = "\n";
$insert_after = 2; //number of paragraphs
// make array of paragraphs
$paragraphs = explode( $split_by, $text);
// if array elements are less than $insert_after set the insert point at the end
$len = count( $paragraphs );
if ( $len < $insert_after ) $insert_after = $len;
// insert $ads_text into the array at the specified point
array_splice( $paragraphs, $insert_after, 0, $ads_text );
// loop through array and build string for output
foreach( $paragraphs as $paragraph ) {
$new_text .= $paragraph;
}
return $new_text;
endif;
return $text;
}
add_filter('the_content', 'insert_ad_block');
I am so close I can feel it. I am trying to get an ad block to show up after say 2 paragraphs. Currently I am using the following code in my functions.php to put the adblock before the last paragraph.
I cannot for the life of me find the correct code to get this done.
function ads_added_above_last_p($text) {
if( is_single() ) :
$ads_text = '<div class="wpselect_middle_content">My Ad Code Here</div>';
if($pos1 = strrpos($text, '<p>')){
$text1 = substr($text, 0, $pos1);
$text2 = substr($text, $pos1);
$text = $text1 . $ads_text . $text2;
}
endif;
return $text;
}
add_filter('the_content', 'ads_added_above_last_p');
If I play with the second $text string and put $pos2 it works perfectly BUT it duplicates all of the text within the post.
Any help would be greatly appreciated.
I am so close I can feel it. I am trying to get an ad block to show up after say 2 paragraphs. Currently I am using the following code in my functions.php to put the adblock before the last paragraph.
I cannot for the life of me find the correct code to get this done.
function ads_added_above_last_p($text) {
if( is_single() ) :
$ads_text = '<div class="wpselect_middle_content">My Ad Code Here</div>';
if($pos1 = strrpos($text, '<p>')){
$text1 = substr($text, 0, $pos1);
$text2 = substr($text, $pos1);
$text = $text1 . $ads_text . $text2;
}
endif;
return $text;
}
add_filter('the_content', 'ads_added_above_last_p');
If I play with the second $text string and put $pos2 it works perfectly BUT it duplicates all of the text within the post.
Any help would be greatly appreciated.
Share Improve this question asked Aug 31, 2013 at 1:47 Matthew SniderMatthew Snider 372 silver badges7 bronze badges1 Answer
Reset to default 3I've found explode() to be useful when trying to break strings apart. This code creates an array of paragraph chunks, inserts the new block after two paragraphs and concatenates it back into a string for output.
function insert_ad_block( $text ) {
if ( is_single() ) :
$ads_text = '<div class="wpselect_middle_content">My Ad Code Here</div>';
$split_by = "\n";
$insert_after = 2; //number of paragraphs
// make array of paragraphs
$paragraphs = explode( $split_by, $text);
// if array elements are less than $insert_after set the insert point at the end
$len = count( $paragraphs );
if ( $len < $insert_after ) $insert_after = $len;
// insert $ads_text into the array at the specified point
array_splice( $paragraphs, $insert_after, 0, $ads_text );
// loop through array and build string for output
foreach( $paragraphs as $paragraph ) {
$new_text .= $paragraph;
}
return $new_text;
endif;
return $text;
}
add_filter('the_content', 'insert_ad_block');
本文标签: adsenseAdding ads after a certain number of paragraphs within Genesis themework
版权声明:本文标题:adsense - Adding ads after a certain number of paragraphs within Genesis themework 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749247002a2339051.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论