想必我們有些朋友也是有需要在WordPress內(nèi)容中添加固定的模塊內(nèi)容推薦內(nèi)容或者是廣告的。我們可以選擇一些插件實(shí)現(xiàn)在內(nèi)容中指定的段落中間插入廣告?;蛘呤怯肈IV浮動標(biāo)簽實(shí)現(xiàn)。這里,前幾天我們有設(shè)置一個谷歌廣告在內(nèi)容的第二段自動添加,這里將找到的方法記錄。
//在文章內(nèi)容的第二段后面插入廣告 add_filter( 'the_content', 'prefix_insert_post_ads' ); function prefix_insert_post_ads( $content ) { $ad_code = '<div class="ggads">谷歌廣告代碼</div>'; if ( is_single() && ! is_admin() ) { // 修改 2 這個段落數(shù) return prefix_insert_after_paragraph( $ad_code, 2, $content ); } return $content; } // 插入廣告所需的功能代碼 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 ); }
這里我們看上面的添加需要設(shè)置的代碼段,以及設(shè)置在幾段開始添加廣告。
這個可以設(shè)置廣告,也可以自動給每個段落中插入需要的內(nèi)容都可以。
評論