本篇文章更新時間:2019/02/16
如有資訊過時或語誤之處,歡迎使用 Contact 功能通知。
一介資男的 LINE 社群開站囉!歡迎入群聊聊~
如果本站內容對你有幫助,歡迎使用 BFX Pay 加密貨幣新台幣 贊助支持。


又正好處理到這件事,心想這件事一旦確定後,更新率實在不高,寫在子主題中 ``functions.php 保持彈性就好!

以下程式碼片段可以針對

標籤來計算段落,並且預設支援平均安插三個廣告碼在一整篇文章內,若該頁面段落數不足 11 則按照每 5 段安插(不會插滿三則)。這是目前測試到比較合理一點的感覺,若有其他規則,看得懂程式碼的人也可以自行調整參數!

廣告碼01

'; $adcode_for_post_center02 = '

廣告碼02

'; $adcode_for_post_center03 = '

廣告碼03

'; $findme = '

'; $positions = array(); $pos = -1; while (($pos = strpos($content, $findme, $pos + 1)) !== false) { $content = substr_replace($content, '[mxp_ad_code_block]', $pos + strlen($findme), 0); } $positions = explode("[mxp_ad_code_block]", $content); //每幾段安插一段廣告 $average = 5; //亦可以判斷超過某段數後使用平均分散方式 $threshold = 11; if (count($positions) > $threshold) { $average = floor(count($positions) / 3); } $content_with_ad = ""; for ($i = 0; $i < count($positions); ++$i) { if ($i == 0) { $content_with_ad .= $positions[$i] . $adcode_for_post_center01; continue; } if ($i == $average) { $content_with_ad .= $positions[$i] . $adcode_for_post_center02; continue; } if ($i == ($average * 2)) { $content_with_ad .= $positions[$i] . $adcode_for_post_center03; continue; } $content_with_ad .= $positions[$i]; } return $content_with_ad; } add_filter('the_content', 'mxp_insert_adcode_in_post');

Gist: link

注意不要複製到第一行 ,會導致在 functinos.php 執行中發生錯誤! 還有當然就是自行替換廣告碼拉~XD


Share:

作者: Chun

資訊愛好人士。主張「人人都該為了偷懶而進步」。期許自己成為斜槓到變進度條 100% 的年輕人。[///////////____36%_________]

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *


文章
Filter
Apply Filters
Mastodon