2011-08-12 19 views
0

我希望把廣告代碼在我的主頁上每4個職位後,如何做到這一點(http://www.news.shreshthbharat.in/bharat)如何在每第n個帖子後創建廣告空間?

這裏是pastbin代碼http://pastebin.com/3vzhZKxE

我我現在正在使用此代碼:

`<?php if ($count==3) { ?> 
<div class="clear"> </div> 
<--- ad code or widget area- - - > 
    <div class="clear"> </div> 
<?php } ?> 
<?php $count = $count + 0; ?>` 

使用此代碼,它會在第二篇文章後顯示廣告。

回答

1

嘗試......

$count = 0; 
if (have_posts()) : while (have_posts()) : the_post(); 
    $count++; 
    $show_ad = $count % 4 == 0; 
    //output the post 
    //show the ad after the post 

%是模運算符。它返回a/b的其餘部分。這個循環的工作是這樣的...

編輯以糾正錯誤MATH

$count = 1, 1 % 4 = 1, so $show_ad = false 
$count = 2, 2 % 4 = 2, so $show_ad = false 
$count = 3, 3 % 4 = 3, so $show_ad = false 
$count = 4, 4 % 4 = 0, so $show_ad = true 
etc... 
+1

當然,你的意思是'1%4 = 1,2%,4 = 1,2,...,4%,4 = 0 '。 – Lstor

+0

嗨請看看我的代碼http://pastebin.com/0u4Tg9Zd 此代碼工作,你可以定義 如果我需要改變每個位置?做什麼 $計數== 3 - [show_ads - 1] $計數== 7 - [show_ads - 2] $計數== 10 - [show_ads - 3] 我需要每個計數不同的廣告,這裏是我使用的代碼 '<?php if($ count == 3 || $ count == 7 || $ count == 10):?> 請看這個http:// wordpress。 org/support/topic/how-to-add-adsence-code-after-every-post-in-home-page?reply = 12 – user891988

+0

@Lstor。 Oy公司。是。謝謝。會改變。 :-) –

相關問題