Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
540 views
in Technique[技术] by (71.8m points)

php - 在X个段落之后插入内容,但不包括某些div类中的段落(Insert content after X number of paragraphs, excluding paragraphs inside a certain div class)

I am using the following code to insert content after the 8th paragraph on the page:

(我正在使用以下代码在页面的第8段之后插入内容:)

add_filter( 'the_content', 'hbg_insert_post_ads_8' );
function hbg_insert_post_ads_8( $content ) {

    $ad_code =  hbg_ad_code();

    if ( is_single() && ! is_admin() ) {
        return hbg_insert_after_paragraph( $ad_code, 8, $content );
    }

    return $content;
}

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

However I would like to exclude paragraphs that are within a certain div so the content is not placed within my highlight or call out boxes.

(但是,我想排除某个div内的段落,以使内容不会放在突出显示或标注框内。)

Perhaps don't count the <p> 's or place the code within a div that has the class "no-ads".

(也许不要计算<p>或将代码放在具有“ no-ads”类的div中。)

Any suggestions?

(有什么建议么?)

Thank you!

(谢谢!)

  ask by Greg Young translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...