在WordPress帖子的段落之间添加广告

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

这是一个讨论广泛的话题。我发现了各种代码片段,可以在WordPress帖子中的段落之间添加广告。但是,这些代码片段都不会对我的内容产生任何影响。

这是我正在使用的摘录:

//Insert ads after second 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, 2, $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 );
}

我已将此代码片段放入我的functions.php文件中。但是,我的帖子页面上没有任何显示。

此主题也在这里讨论:Adding Ads to Second Paragraph of WordPress Page

我将非常感谢您提供任何建议。

谢谢!

php wordpress ads paragraph
1个回答
0
投票

[您好,您可以使用插件而不是使用代码。请参阅此插件:

https://wordpress.org/plugins/insert-post-ads/

© www.soinside.com 2019 - 2024. All rights reserved.