2016-06-26 4 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

Просто посчитайте посты с помощью '$ я = 0 ' в начале и '$ i ++' в вашем цикле. И тогда вы просто используете 'if ($ i% 3)', чтобы посмотреть, показывать ли вам рекламу. – jornane

+0

@jornane Я очень не уверен, как отредактировать это в цикле. Я читал о цикле, так как вы прокомментировали, но все еще совершенно не уверены в том, как его редактировать без него. – Ritzy

+0

@Ritzy В стиле того, что вы «Я делаю, я бы дал приращение' the_post' и сделал «post_advertisement», который выполняет проверку. Не совсем уверен, как работают ваши 'has_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

Nevermind нашел решение! это было потому, что у меня было настроено отображать только две записи на моей индексной странице по какой-либо причине, большое спасибо 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 : ?> 

Смежные вопросы