2016-06-26 64 views
1

我目前首次致力於WordPress網站,並且遇到了一些問題,以瞭解如何實現我的目標。我希望它在每三分之一後顯示一個橫幅廣告,然後從我現有的文章中遺漏的位置繼續。在每發佈一篇文章後添加替代文本

因爲我可能措辭不佳,我會在下面顯示一個示例圖像。

下面是我用我的index.php文件中的電流環路文件。

<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 

<?php 

    $title = htmlentities(get_the_title()); 
    $str = explode ("&amp;#8211;", $title); 

    $artist = preg_replace('#\[[a-zA-Z].*\]#','',$str[0]); 
    $song = preg_replace('#\[[a-zA-Z].*\]#','',$str[1]); 

?> 
<div class="album-meta" style="border-bottom: 1px solid #eee;"> 
    <div class="cover"> 
     <a href="<?php the_permalink(); ?>" style="text-decoration: none; color: #757575"><img width="90px" height="90px" src="<?php $feat_image = wp_get_attachment_url(get_post_thumbnail_id($post->ID)); echo ''.$feat_image.''; ?>" alt="<?php the_title(); ?>"></a> 
    </div> 
<div class="metadata"> 
    <a href="<?php the_permalink(); ?>" style="text-decoration: none; color: #757575"><p><i style="font-size: 13.7px;"><?php print $song; ?></i></p> 
    <p><strong style="font-size: 15px;"><?php print $artist; ?></strong></p> 
    </a> 
    <p><a href="http://linkshrink.net/zPog=<?php the_permalink(); ?>" style="color: #fff; background: #4E76C9; width: 200px; height: 50px;padding: 5px;line-height: 50px;font-size: 20px;font-weight: bold; border: none;text-shadow: 0px 1px 0px #3170DD;box-shadow: inset 0px 0px 0px 1px #3170DD;border-radius: 3px 3px; cursor: pointer; text-decoration: none;">Download</a> 
</div> 
</div> 
<?php endwhile; else : ?> 



<p><?php _e('Sorry, no posts matched your criteria.'); ?></p> 



<?php endif; ?> 
+3

使用'$ I = 0'就指望職位在你的循環中開始和'$ i ++'。然後你只需使用if($ i%3)'來查看你是否應該顯示廣告。 – jornane

+0

@jornane我非常不確定如何編輯這個循環..我一直在閱讀有關循環,因爲你評論,但仍然完全不知道如何編輯它沒有它搞亂.. – Ritzy

+0

@Ritzy在你的風格'在做,我會讓'the_post'增量並做一個'post_advertisement'來完成檢查。不確定你的'have_posts'是如何工作的,所以我可能在這裏錯了。 – jornane

回答

1

你可以像下面: -

<?php $i = 0; // create a counter?> 
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 

<?php 

    $title = htmlentities(get_the_title()); 
    $str = explode ("&amp;#8211;", $title); 

    $artist = preg_replace('#\[[a-zA-Z].*\]#','',$str[0]); 
    $song = preg_replace('#\[[a-zA-Z].*\]#','',$str[1]); 

?> 
<div class="album-meta" style="border-bottom: 1px solid #eee;"> 
    <div class="cover"> 
     <a href="<?php the_permalink(); ?>" style="text-decoration: none; color: #757575"><img width="90px" height="90px" src="<?php $feat_image = wp_get_attachment_url(get_post_thumbnail_id($post->ID)); echo ''.$feat_image.''; ?>" alt="<?php the_title(); ?>"></a> 
    </div> 
    <div class="metadata"> 
     <a href="<?php the_permalink(); ?>" style="text-decoration: none; color: #757575"><p><i style="font-size: 13.7px;"><?php print $song; ?></i></p> 
     <p><strong style="font-size: 15px;"><?php print $artist; ?></strong></p> 
     </a> 
     <p><a href="http://linkshrink.net/zPog=<?php the_permalink(); ?>" style="color: #fff; background: #4E76C9; width: 200px; height: 50px;padding: 5px;line-height: 50px;font-size: 20px;font-weight: bold; border: none;text-shadow: 0px 1px 0px #3170DD;box-shadow: inset 0px 0px 0px 1px #3170DD;border-radius: 3px 3px; cursor: pointer; text-decoration: none;">Download</a> 
    </div> 
</div> 
<?php if($i%3 ==0 && $i >0){ // check you reached to third div or not?> 
<!-- write the html of advertisement div -------> 
<?php $i++;} ?> <!-- increase counter --> 
<?php endwhile; else : ?> 



<p><?php _e('Sorry, no posts matched your criteria.'); ?></p> 



<?php endif; ?> 
+0

這種方法正是我這樣做的,但它只是在每篇文章後面返回'html',而不是在每篇文章之後。 – Ritzy

+0

沒關係找到解決方案!這是因爲我已經設置爲在我的索引頁上只顯示兩個帖子,因爲什麼原因,非常感謝Anant! – Ritzy

+0

在上面的代碼中,第一次顯示「廣告div」,所以我們需要添加一個條件來避免第一次代碼。 if($ i%3 == 0 && $ i 1 = 0) –

1

嘗試......

<?php 

$counter = 0; 

if (have_posts()) : while (have_posts()) : the_post(); ?> 

<?php 

    $title = htmlentities(get_the_title()); 
    $str = explode ("&amp;#8211;", $title); 

    $artist = preg_replace('#\[[a-zA-Z].*\]#','',$str[0]); 
    $song = preg_replace('#\[[a-zA-Z].*\]#','',$str[1]); 

if($counter == 3){ ?> 

********** Advertisement code write here *********** 
<?php 
} 


?> 
<div class="album-meta" style="border-bottom: 1px solid #eee;"> 
    <div class="cover"> 
     <a href="<?php the_permalink(); ?>" style="text-decoration: none; color: #757575"><img width="90px" height="90px" src="<?php $feat_image = wp_get_attachment_url(get_post_thumbnail_id($post->ID)); echo ''.$feat_image.''; ?>" alt="<?php the_title(); ?>"></a> 
    </div> 
<div class="metadata"> 
    <a href="<?php the_permalink(); ?>" style="text-decoration: none; color: #757575"><p><i style="font-size: 13.7px;"><?php print $song; ?></i></p> 
    <p><strong style="font-size: 15px;"><?php print $artist; ?></strong></p> 
    </a> 
    <p><a href="http://linkshrink.net/zPog=<?php the_permalink(); ?>" style="color: #fff; background: #4E76C9; width: 200px; height: 50px;padding: 5px;line-height: 50px;font-size: 20px;font-weight: bold; border: none;text-shadow: 0px 1px 0px #3170DD;box-shadow: inset 0px 0px 0px 1px #3170DD;border-radius: 3px 3px; cursor: pointer; text-decoration: none;">Download</a> 
</div> 
</div> 
<?php 
$counter++; 
endwhile; else : ?> 

相關問題