注入在X影像後,職位代碼我們在WordPress的功能,在帖子顯示的代碼塊3段後:如何在WordPress的
add_filter('the_content', 'wpse_ad_content');
function wpse_ad_content($content)
{
if (!is_single()) return $content;
$paragraphAfter = 3; //Enter number of paragraphs to display ad after.
$content = explode("</p>", $content);
$new_content = '';
for ($i = 0; $i < count($content); $i++) {
if ($i == $paragraphAfter) {
$new_content.= '<div style="col-xs-12">';
$new_content.= 'code here';
$new_content.= '</div>';
}
$new_content.= $content[$i] . "</p>";
}
return $new_content;
}
我們正在努力完成這一功能包括與顯示另一塊代碼僅在帖子中包含至少8個圖像(通過圖像瞭解每個
<img src=...
)。
該塊應該在圖像nº8之後顯示,如果它們不是8個圖像,則附加功能不會顯示任何內容。
-----編輯1: 與@TimTroiano解決方案
add_filter('the_content', 'wpse_ad_content');
function wpse_ad_content($content)
{
if (!is_single()) return $content;
$paragraphAfter = 3; //Enter number of paragraphs to display ad after.
$imagesAfter = 7; //Enter number of images to display code snippet after.
$content = explode("</p>", $content);
$new_content = '';
for ($i = 0; $i < count($content); $i++) {
if ($i == $paragraphAfter) {
$new_content.= '<div class="col-lg-12" style="text-align: center; padding-top: 10px; margin-bottom: 20px;">';
$new_content.= '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- La nube sobre título -->
<ins class="adsbygoogle"
style="display:block"
data-ad-client=""
data-ad-slot="7588478595"
data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>';
$new_content.= '</div>';
}
$new_content.= $content[$i] . "</p>";
if ($imagesAfter > 0) {
$imagesAfter -= 1;
} else {
$new_content.= '<div class="col-lg-12" style="text-align: center; padding-top: 10px; margin-bottom: 20px;">';
$new_content.= '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- La_Nube_4_Anuncio -->
<ins class="adsbygoogle"
style="display:block"
data-ad-client=""
data-ad-slot="3663644595"
data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>';
$new_content.= '</div>';
$imagesAfter = 7; //Reset number of images to display code after.
}
}
return $new_content;
}
努力之後我們注意到一個奇怪的行爲。 1.考慮文本的段落,就好像它們是圖片一樣。 2.此外,如果更多圖像或段落(8的倍數)並且應該只出現一次,則重複該代碼塊。
此論壇不適合人們爲您編寫代碼。請發佈你有什麼讓人們可以幫助你。 –
這就是說,如果你已經有你的PHP顯示的圖像與foreach循環或類似的東西只計算一個新的變量,每次達到一定的數字插入代碼並重置變量。防爆。 $ i = 0; foreach($ result as $ row){ print' 2){ ** **在此處插入代碼**; $ i = 0; } } –
嗨@TimTroiano對不起。我將編輯該問題。 –