在wordpress的最后一段之前放置广告

问题描述 投票:1回答:3

以下是第1,第2,第3段之后的地方广告代码。但我想在我的wordpress帖子的最后一段之前放置广告。有没有办法做到这一点?

<?php

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 );
}
?>
wordpress adsense
3个回答
1
投票

我在我的名为All BD Results和etunescafe的网站中通过此代码设置了Adsense代码。您只需在您的网站functions.php N.B中添加此代码。在添加代码之前,请替换您的或添加您的adsense代码。通过这篇文章就足够了。 How to Set Adsense Ads Right Before The Last Paragraph

function ads_added_above_last_p($text) {
    if( is_single() ) :
        $ads_text = '<div class="a" style="text-align: center;">Your Adsense Code</div>';
        if($pos1 = strrpos($text, '<p>')){
            $text1 = substr($text, 0, $pos1);
            $text2 = substr($text, $pos1);
            $text = $text1 . $ads_text . $text2;
        }
    endif;
    return $text;
    }
add_filter('the_content', 'ads_added_above_last_p');

1
投票
add_filter('the_content', 'ad_signal');
function ad_signal($content)
{
    if (!is_single()) return $content;
    $tent = get_the_content();
    $content = explode("</p>", $content);
    $ss = count($content);
    $ns = $ss-1;   //**before last paragraph in wordpress  **
    $new_content = '';
    for ($i = 0; $i < count($content); $i++) {
         if ($i == $ns ) {

            $new_content.= ' <div style="text-align:center;">';
            $new_content.= '<h1> Your ads Here ! </h1>';
            $new_content.= '</div>';
        }


        $new_content.= $content[$i] . "</p>";
    }

    return $new_content;
}

0
投票

嘿伙计,所以我遇到了同样的问题,并设法解决它将你的答案合二为一。

执行所有处理的函数已更改,以便计算explode方法创建的所有段落。然后添加另一个if语句来评估您何时在所需位置。

function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {

    $closing_p = '</p>';

    $paragraphs = explode( $closing_p, $content );

    $last_paragraph_index = count($paragraphs);

    foreach ($paragraphs as $index => $paragraph) {

        if ( trim( $paragraph ) ) {
            $paragraphs[$index] .= $closing_p;
        }

        if ( $paragraph_id == $index + 1 ) {
            $paragraphs[$index] .= $insertion;
        }

        else if( $last_paragraph_index + $paragraph_id == $index + 1){
             $paragraphs[$index] .= $insertion;
        }

    }


    return implode( '', $paragraphs );
}

要使用新功能,您必须使用负数来调用它。

以这种方式考虑这个功能,如果你想从内容的顶部开始使用正数,如果你想从内容的底部开始使用负数。

请记住,即使评估是针对您的决定点,例如在最后一段之前,添加到数组的条件使用我们都知道的索引为零。

这只是意味着要在第1段之后,你必须在调用函数时使用数字2,并且在最后一个段落之前必须使用-2,依此类推。

这是我的例子,如果它存在于文章中,则添加读取下一篇文章

add_filter( 'the_content', 'prefix_insert_next_article_banner' );

function prefix_insert_next_article_banner( $content ) {

    if ( is_single() && ! is_admin() && get_next_post_link() ) {

           $next_banner  = '<div class="next-banner"> <span> Read Next </span>';
           $next_banner .= '<a href="' . $prev = get_permalink(get_adjacent_post(false,'',false)) .'"> ' . get_the_title(get_adjacent_post(false,'',false)) .' </a>';
           $next_banner .= '</div>';

           return prefix_insert_after_paragraph( $next_banner, -2, $content );

    }

    return $content;
}
© www.soinside.com 2019 - 2024. All rights reserved.