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



因此,在寻找解决问题的方法时,我偶然发现了这段代码,由 Sally CJ 提供,但我不知道如何联系他,因为我是新用户,无法在线程中发表评论。所以我想也许你们中的一些人或 Sally 本人可以帮助我通过"帖子 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;
}

您可以尝试以下操作:

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

最新更新