如何在此自定义广告脚本中按帖子ID排除帖子?

时间:2019-01-10 11:53:45

标签: php wordpress ads

因此,在寻找解决方案时,我偶然发现了这段代码,这是由Sally CJ提供的,但是由于我是新用户并且无法在其中发表评论,所以我不知道如何与他联系线。 因此,我认为也许您中的某些人或Sally本人可以通过此脚本中的“ post id”排除我的某些帖子。

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;
}

1 个答案:

答案 0 :(得分:0)

您可以尝试以下操作:

function prefix_insert_post_ads( $content ) {
    global $post;

    $excluded_ids = array(5, 6, 7, 8);
    if (in_array($post->ID, $excluded_ids)) {
       return $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;
}