【发布时间】:2015-08-30 18:55:45
【问题描述】:
我为单个页面使用了两个不同的模板。一个有侧边栏,另一个没有侧边栏。
现在我只想在侧边栏模板中注入广告代码。
谁能给我这个的代码。
注意:
-
广告会自动插入到第二个段落之后。我有代码。
add_filter('the_content', 'prefix_insert_post_ads');
function prefix_insert_post_ads( $content ) { if ( is_single() && ! is_admin()) { return prefix_insert_after_paragraph( $ad_code, 2, $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 ); }
以上代码在侧边栏模板和无侧边栏模板中均显示广告代码。但实际上我只想在侧边栏模板中显示广告代码。谁能帮我看看怎么弄。
【问题讨论】:
-
您需要在此处提供更多信息,您应该将您的 single.php 或任何模板上传到 pastebin。侧边栏显示和不显示有什么区别?它们是由小部件等设置的吗?