WordPress.org

Ready to get started?Download WordPress

Forums

Place DIV tag at the star of every specific number of posts, is it possible? (5 posts)

  1. LockeAG4
    Member
    Posted 3 years ago #

    Hi ..

    I would like to know if it's possible to add for example:

    a <div class="pages"></div>

    every 4 post

    I mean if I use this:

    <?php $my_query = new WP_Query('cat=2&showposts=4p&orderby=id&order=asc'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
     <div>
    <?php the_post_thumbnail(); ?>
    <h1 class="other-item"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    </div>
    <?php endwhile; ?>

    I get this, repeated 3 more times :

    <div>
    <a href="#" title=""><img src="#" class="attachment-post-thumbnail wp-post-image" alt="" title="" /></a>
    <h1 class="other-item"><a href="#">EXPAMLE</a></h1>
    </div>

    But is possible to place a :

    <div class="pages"></div>

    at the first post and then place it util every 2th post, like

    <div class"pages">
    
    <div>
    <a href="#" title=""><img src="#" class="attachment-post-thumbnail wp-post-image" alt="" title="" /></a>
    <h1 class="other-item"><a href="#">EXAMPLE</a></h1>
    </div>
    
    <div>
    <a href="#" title=""><img src="#" class="attachment-post-thumbnail wp-post-image" alt="" title="" /></a>
    <h1 class="other-item"><a href="#">EXAMPLE</a></h1>
    </div>
    
    </div> <!--- End Pages -->
    
    <div class"pages"> <!--- Pages is again placed after 2 posts -->
    
    <div>
    <a href="#" title=""><img src="#" class="attachment-post-thumbnail wp-post-image" alt="" title="" /></a>
    <h1 class="other-item"><a href="#">EXAMPLE</a></h1>
    </div>
    
    <div>
    <a href="#" title=""><img src="#" class="attachment-post-thumbnail wp-post-image" alt="" title="" /></a>
    <h1 class="other-item"><a href="#">EXAMPLE</a></h1>
    </div>
    
    </div> <!--- End Second Instance Pages -->

    Sorry to bother with this kind of things , but hope somebody can give a answer of this..

    thanks in advance

  2. esmi
    Theme Diva & Forum Moderator
    Posted 3 years ago #

    Try:

    <?php $my_query = new WP_Query('cat=2&showposts=4p&orderby=id&order=asc'); $c = 0;?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); $c++;?>
    <?php if( $c % 2 ) echo '<div>';
    else echo '<div class="pages">';?>
    <?php the_post_thumbnail(); ?>
    <h1 class="other-item"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    </div>
    <?php endwhile; ?>
  3. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    a bit expanded to close the div .pages even when an odd number of posts are in the loop:

    <?php $my_query = new WP_Query('cat=2&showposts=4p&orderby=id&order=asc');
    $i = 0; // initialize counter
    ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <?php if( $i%2 == 0 ) { echo '<div class="pages">'; } ; // open div ?>
     <div>
    <?php the_post_thumbnail(); ?>
    <h1 class="other-item"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    </div>
    <?php if( $i%2 != 0 ) { echo '</div><!-- .pages end-->'; }; //close div
    $i++; //increase counter
    <?php endwhile; ?>
    <?php if( $i%2 != 0 ) { echo '</div><!-- .pages end-->'; }; //close div if odd number of posts in loop ?>
  4. LockeAG4
    Member
    Posted 3 years ago #

    Esmi , I'm to close to get this thanks to you...

    I take the example above and use it like this:

    <?php $my_query = new WP_Query('cat=6&showposts=24p&orderby=id&order=asc'); $c = 0;?>
                    <?php while ($my_query->have_posts()) : $my_query->the_post(); $c++;?>
                    <?php if( $c == 1) echo '<div class="page">'; elseif ($c == 6) echo '</div>';?>
                    <?php if( $c == 6) echo '<div class="page">'; elseif ($c == 12) echo '</div>';?>
                    <?php if( $c == 12) echo '<div class="page">'; elseif ($c == 18) echo '</div>';?>
                    <?php if( $c == 18) echo '<div class="page">'; elseif ($c == 24) echo '</div>';?>
                    <div>
                    <?php the_post_thumbnail(); ?>
                    <h1 class="other-item"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
                    </div>
    
                    <?php endwhile; ?>

    But how can I make a simple way to achive this?

  5. LockeAG4
    Member
    Posted 3 years ago #

    oh alchymyth, I didn't see your post, I was writing my last post , let's try your your idea..

    Thanks very much

Topic Closed

This topic has been closed to new replies.

About this Topic