我想通过谷歌在文本内容之间显示广告,比如在第一段后第二段后第三段和最后一段末尾显示3个广告。
我知道我必须修改single.php
但作为php的新手,我无法做到这一点。
如果有人能帮我做这件事,我会很有帮助的。谢谢
我想通过谷歌在文本内容之间显示广告,比如在第一段后第二段后第三段和最后一段末尾显示3个广告。
我知道我必须修改single.php
但作为php的新手,我无法做到这一点。
如果有人能帮我做这件事,我会很有帮助的。谢谢
您可以使用短代码或the_content
filter. 我想the_content
过滤器更好,因为您的帖子中不引入任何字符串,所以如果需要,可以导出内容并在其他平台中使用。例如,在第一个parragraph之后显示一块adsense:
add_filter( \'the_content\', \'tbn_ads_inside_content\' );
function tbn_ads_inside_content( $content ) {
$output = $content;
//We don\'t want to modify the_content in de admin area
if( !is_admin() ) {
$ads = "<p>your_ads_code</p>";
$p_array = explode(\'</p>\', $content );
$p_count = 1;
if( !empty( $p_array ) ){
array_splice( $p_array, $p_count, 0, $ads );
$output = \'\';
foreach( $p_array as $key=>$value ){
$output .= $value;
}
}
}
return $output;
}
下面是我用来在帖子第二段之后添加小部件的函数。这只适用于第一篇文章和单个文章,因此如果需要在所有文章中显示这一点,则需要对其进行一些调整。您应该能够修改此选项以满足您的需要。希望这有帮助
// Add advertising widget within the firts post and single posts
function pietergoosen_insert_content_after_second_paragraph_filter( $content ) {
static $first;
$first = (!isset($first)) ? true : false;
if (true == $first) {
ob_start();
echo \'<div class="widget-box">\';
dynamic_sidebar( \'sidebar-19\' );
echo \'</div><!-- end .widget-box -->\';
$new_content = ob_get_clean();
if ( ! is_admin() ) {
return pietergoosen_insert_content_after_second_paragraph( $new_content, 2, $content );
}
}
return $content;
}
add_filter(\'the_content\',\'pietergoosen_insert_content_after_second_paragraph_filter\');
// Paragraph explode and insert
function pietergoosen_insert_content_after_second_paragraph( $new_content, $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] .= $new_content;
}
}
return implode( \'\', $paragraphs );
}
我正在开发一个简单的插件,我需要过滤类别页面中的单个\\u cat\\u标题,在每个类别页面中添加一些html。这样做:function subtitleCategory ($title) { return $title.\"</div></div></div><div class=\\\"container_24\\\"><h2 id=\\\"category-subtitle\\\">TEST1</h2></