简体   繁体   中英

Add Static content inbetween Wordpress posts in While loop

I need to add some static content into a div inbetween each Wordpress. Here is what I have so far...

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

So in <div class="masonryImage"> I need the odd numbered indexes to load Wordpress posts, and the even indexes to load the repeated static content. This may explain it more...

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

Any way to do this? Thanks in advance!

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM