我尝试使用此功能在列表中添加的每个3,6,9特定标记后插入广告: (用获取内容编辑的代码)
add_filter( 'get_the_content', 'prefix_insert_post_ads2' );
function prefix_insert_post_ads2( $content ) {
//The last condition here ensures that ads are only added to posts
if ( is_archive()) {
return prefix_insert_ads( $content );
}
return $content;
}
function prefix_insert_ads( $content ) {
$closing_p = '</em>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
$paragraphs[$index] .= $closing_p;
if ( in_array($index, array(3, 6, 9)) ) {
//Replace the html here with a valid version of your ad code
$paragraphs[$index] .= '<div>Ad goes here</div>';
}
}
return implode( '', $paragraphs );
}
但是不在分类学列表中工作,我尝试过如果是归档,如果是税,如果是归档,如果有术语,即使是分页!,但是根本不起作用。但奇怪的是,在页面中的另一个列表中工作。是一个wp事件管理器插件分类,也许我需要创建一个占位符¿?,我不知道为什么不工作。有什么想法吗?
更新:
我现在正在使用此功能,但也无法使用此功能:
//Insert ads
function prefix_insert_after_paragraph( $ads, $content ) {
if ( ! is_array( $ads ) ) {
return $content;
}
$closing_p = '</em>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
$n = $index + 1;
if ( isset( $ads[ $n ] ) ) {
$paragraphs[$index] .= $ads[ $n ];
}
}
return implode( '', $paragraphs );
}
add_filter( 'the_content', 'prefix_insert_ads' );
function prefix_insert_ads( $content ) {
if ( is_archive() && ! is_admin() ) {
$content = prefix_insert_after_paragraph( array(
'3' => '<div>1</div>',
'6' => '<div>2</div>',
'9' => '<div>3</div>',
), $content );
}
return $content;
}