【问题标题】:Insert content after X number of paragraphs, excluding paragraphs inside a certain div class在X个段落之后插入内容,不包括某个div类内的段落
【发布时间】:2019-11-23 18:52:01
【问题描述】:

我正在使用以下代码在页面第 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 );
}

但是我想排除某个 div 中的段落,这样内容就不会放在我的突出显示或标注框中。也许不要计算&lt;p&gt; 或将代码放在具有“无广告”类的 div 中。有什么建议吗?

谢谢!

【问题讨论】:

    标签: php html wordpress


    【解决方案1】:

    您可以将 HTML 转换为 DOM。 然后遍历 DOM 并在任何你想要的地方添加新元素。 然后将 DOM 转换回 HTML

    这是遍历节点的函数示例:

    function insert_node($parent, $elements_to_ignore, & $at, $newNode, $nested_level){
        if(!property_exists($parent,"childNodes") || $parent->childNodes === NULL ){
        return;
        }
        foreach($parent->childNodes as $element ){
        //process childs
    
        if( get_class($element) != "DOMElement"){
            continue;
        }
    
        if(!property_exists($element,"tagName")){
            continue;
        }
        $tag = $element->tagName;
        $class = $element->getAttribute("class");
        if(in_array($class, $elements_to_ignore)){
            echo "ignoring node $class\n";
            continue;
        }
        echo str_repeat("-",$nested_level)."-> tagName[$at] :  class: ".get_class($element)." ".$tag."\n";
        if($tag == "p"){
            $at--;
            if($at==0){
                echo "adding node\n";
                $parent->insertBefore($newNode,$element);
            }
        }
        insert_node($element, $elements_to_ignore, $at,$newNode,$nested_level+1);
    
        }
    }
    

    这是一个如何使用它的示例:

    $body="<p>1</p><p>2</p><div class=\"bad-class1\"><p>3</p><p>4</p><p>5</p></div><div class=\"good-class\"><p>6</p></div><p>7</p><a href=\"blahblah\">sdsds</a><p>8</p>";
    
    $dom =  new DOMDocument();
    $dom->loadHTML($body);
    
    $root_element = $dom->getElementsByTagName('body')[0];
    
    $new_element = new DOMElement("p","extra text");
    $at = 4;// insert new element before 4-th paragraph
    insert_node($root_element,["bad-class1","no-ads"], $at, $new_element,0);
    
    echo $dom->saveHTML();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多