您可能已经注意到,我们已经调整了我们如何展示我们的AdSense广告。我们希望每一个附近的文章底部的一些小纯文字广告, 这将自动添加。这摆弄了一点点, but eventually we developed a solution which works well and doesn’t seem to knock page processing times much. Simply add the following code to your theme’s functions.php (不要忘记更改您的AdSense标识)
Updated 12-Nov-2017 as the code wasn’t working correctly as previously displayed. 我还添加 2 other functions that I use, one to insert adverts via shortcode, and another to add an advert where the more is removed when a post is shown in full
/************************************************************************\ * 对于插入的Adsense短代码 2 小广告通过短码的任何地方 * \************************************************************************/ 功能Google_Adsense( $的ATT, $内容= NULL ) { 回归“<DIV CLASS ="一个" 风格="文本对齐: 中央;"><DIV ID ="谷歌-1"></DIV><P类="明确: 两"></p></DIV>“; } add_shortcode('google_adsense', 'Google_Adsense'); /******************************************\ * 显示 2 在更小的标签AdSense广告 * \******************************************/ 的add_filter('内容', 'adsense_added_at_more_tag'); // 此功能与您的AdSense代码替换您的多个标签. 功能adsense_added_at_more_tag($文本) { 如果( is_single() ) : $ads_text ='<DIV ID ="谷歌" CLASS ="一个" 风格="文本对齐: 中央;"><DIV ID ="谷歌-1"></DIV><P类="明确: 两"></p></DIV>“; $POS1 = strpos($文本, “<跨度ID ="更多-'); //" 如果($POS1 === FALSE) $文字= $ ads_text . $文本; 其他 { $POS2 = strpos($文本, “</跨度>“, $POS1); $文本1 = SUBSTR($文本, 0, $POS1); $文本2 = SUBSTR($文本, $POS2 + 7); $文字= $文本1 . $ads_text . $文本2; } 万一; 返回$文本; } /*******************************************************\ * 显示最后一个段落上方独大的AdSense广告 * \*******************************************************/ 的add_filter('内容', 'gads_added_above_last_para'); 功能gads_added_above_last_para($ytext) { 如果( is_single() ) : $yads_text ='<DIV CLASS ="一个" 风格="文本对齐: 中央;"><DIV ID ="谷歌-2"></DIV><P类="明确: 两"></p></DIV>“; 如果($ypos1 = strrpos($ytext, “</p>“)){ $ytext1 = SUBSTR($ytext, 0, ($ypos1 + 4)); $ytext2 = SUBSTR($ytext, ($ypos1 + 4)); $ytext = $ ytext1 . $yads_text . $ytext2; } 万一; 返回$ ytext; }
请通过下面的评论给我们你的想法! 如果您想订阅,请使用菜单上的订阅链接右上方. 您还可以通过使用下面的链接社会分享这与你的朋友. 干杯.
I don’t know but your code was not working in my case. 所以, I replaced it with other code. If anyone else is facing the same problem, 那么你可以使用下面的代码.
function insert_ad_block( $文本 ) {
如果 ( is_single() ) :
$ads_text ='我的广告代码这里“;
$split_by =“ n”;
$insert_after = 2; //段数
// 使段落阵列
$paragraphs = explode( $split_by, $文本);
// if array elements are less than $insert_after set the insert point at the end
$LEN =计数( $paragraphs );
如果 ( $只 < $insert_after ) $insert_after = $ LEN;
// insert $ads_text into the array at the specified point
array_splice( $paragraphs, $insert_after, 0, $ads_text );
// 通过阵列和构建字符串输出回路
的foreach( $paragraphs as $paragraph ) {
$new_text .= $paragraph;
}
返回$ NEW_TEXT;
万一;
返回$文本;
}
的add_filter(‘the_content’, ‘insert_ad_block’);
有没有办法做了之后说这 2nd 段,而不是之前的最后一个?
绝对. 你只需要稍微调整代码. 我使用
strrpos
其中发现的最后一次出现<p>
如果你想找到的第二次出现
<p>
更换线路 13 从上面的代码与...如果($POS1 = strpos($文本, “<p>“, strpos($文本, “<p>“) + 3)){
伟大的工作人. 多谢. Do you have any idea how to place a horizontal line above and below the ad unit?
确定. 添加了几个 <HR />的 - 1 at the start of the value of $ads_text and the other at the end.
E.G. 更换
同