在While循环中的Wordpress帖子之间添加静态内容

时间:2013-10-01 08:54:28

标签: php wordpress loops

我需要在每个Wordpress之间的div中添加一些静态内容。这是我到目前为止所拥有的......

query_posts('cat=technology');?>
<?php 
  $i=1;
  if ( have_posts() ) : while ( have_posts( ) ) : the_post();
    $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
    echo $i++; //Echoing $i to see if incrementing works ok
?>
<div class="masonryImage" style="width: 300px; height:250px;">
<img width="300" height="250" src= "<? echo $feat_image ?>" alt="<? echo the_title(); ?>" />
</div>
<?php endwhile; endif; ?>

所以在<div class="masonryImage">我需要奇数编号的索引加载Wordpress帖子,偶数索引加载重复的静态内容。这可以解释得更多......

<div class="masonryImage"> //Index 1
WORDPRESS POST
</div>
<div class="masonryImage"> //Index 2
STATIC CONTENT
</div>
<div class="masonryImage"> //Index 3
WORDPRESS POST
</div>

有什么办法吗?提前谢谢!

1 个答案:

答案 0 :(得分:3)

试试这个:

query_posts('cat=technology');?>
<?php 
$i=1;
if ( have_posts() ) : while ( have_posts( ) ) : the_post();
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
echo $i++; //Echoing $i to see if incrementing works ok
?>
<div class="masonryImage" style="width: 300px; height:250px;">
<img width="300" height="250" src= "<? echo $feat_image ?>" alt="<? echo the_title(); ?>" />
</div>
<?php 
if ($i%2 == 0){ ?> // this block here will be executed every second time
    <div class="masonryImage"> //Index 2
       STATIC CONTENT
    </div>
<?php }
?>
<?php endwhile; endif; ?>