Добавление рекламы после первого и второго абзаца поста WordPress

Я надеюсь, что кто-то может помочь с этим вопросом. У меня есть следующий рабочий код в моем файле functions.php, чтобы разместить рекламу Adsense после первого абзаца каждого поста. Я надеюсь, что кто-то знает, как настроить этот код, чтобы я мог добавить еще одно объявление после второго абзаца. Итак, в двух словах, я хочу рекламу после первого и второго абзаца.

Спасибо ….. код ниже.

//Insert ads after first paragraph of single post content.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {

$ad_code = '<div>Ads code goes here</div>';

if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 1, $content );
}

return $content;
}

// Parent Function that makes the magic happen
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 );
}

0

Решение

Ниже приведен весь код:

function prefix_insert_after_paragraph2( $ads, $content ) {
if ( ! is_array( $ads ) ) {
return $content;
}

$closing_p = '</p>';
$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_post_ads' );
function prefix_insert_post_ads( $content ) {
if ( is_single() && ! is_admin() ) {
$content = prefix_insert_after_paragraph2( array(
// The format is: '{PARAGRAPH_NUMBER}' => 'AD_CODE',
'1' => '<div>Ad code after FIRST paragraph goes here</div>',
'2' => '<div>Ad code after SECOND paragraph goes here</div>',
), $content );
}

return $content;
}
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector