2016-06-26 6 views
1

私は現在、初めてWordPressサイトで作業しており、私が目指していることを達成する方法を理解する上でいくつかの問題があります。 3枚ごとにバナー広告を表示してから、既存の投稿に残っている場所から続行したいと思います。3回ごとに代替テキストを入力する

私はおそらくこれをよく言わなかったので、私は以下の例のイメージを表示します。

は、ここで私は私の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 ++'が始まります。そして、単に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

このメソッドは、私がやっていた方法とまったく同じですが、すべての3番目のポストの後ではなく、すべてのポストの後に 'html'を返します。 – Ritzy

+0

Nevermindが解決策を見つけました!なぜなら何故私のインデックスページに2つのポストしか表示しないように設定していたからです。本当にありがとうございました! – Ritzy

+0

上記のコードでは、最初の "広告div"が表示されるので、最初のコードを避けるために1つの条件を追加する必要があります。 if($ i%3 == 0 && $ i 1 = 0) –

1

ただ、 `$ I = 0 'を使用して記事を数える

<?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 : ?> 

関連する問題