• i want my loop to display 4 posts, then echo </div><div class=small4″>
    until there is no more posts. for now, it works only one time.

    my code:

    <div class="small4"> 
    
    <?php $split = ceil( count($posts) / 4 ); ?>
    
    <?php if (is_category('photos1')) {query_posts ('category_name=photos1');} ?>
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
    
       <div class="post" id="post-<?php the_ID(); ?>">
         <div class="entry">
           <?php the_content(''); ?>
         </div>
       </div>
       <div class="spacer"></div>
    
    <?php if( $post == $posts[$split] ) :
     echo "</div><div class=\"small4\">" ?>
    
    <?php  endif; ?>	
    
    <?php endwhile; endif; ?>
    
    </div>

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter baalinato

    (@baalinato)

    I found the answer

    <div class="small4"> 
    
    <?php if (is_category('photos1')) {query_posts ('category_name=photos1');} ?>
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
    
    			<div class="post" id="post-<?php the_ID(); ?>">
    
    				<div class="entry">
    					<?php the_content(''); ?>
    
    					<?php edit_post_link('Edit', '<p>', '</p>'); ?>
    				</div>
    
    			</div>
    		<div class="spaceit"></div>
    
    <strong><?php
    $count++;
    $y = 4;
    $r = fmod($count, $y);
    if ($r==0)
    {
    echo '</div><div class="small4">';
    }
    ?></strong>
    
    		 <?php endwhile; endif; ?>
    
    		</div>

    I’m glad you made it yourself. Do you mind share why do you wanna do so? Any demo?

    Regards,
    [sig moderated. Resist the urge please]

    Another way to loop is like this:

    $count++;
    if ($count==3)
    {
    $count=0;
    echo '</div><div class="small4">';

    What does it mean? If count is 3, that mean every 4th times it will reset to 0 and count again.

    🙂 maybe it will be faster than doing fmod function 😉

    Thread Starter baalinato

    (@baalinato)

    i tried urs and i had to change the 3 for a 4 to make it work. seems longuer. maybe an illusion.

    here’s a demo : http://baa-li.com/test/photos1/

    the code on the template category :

    <?php get_header(); ?>
    
    <!--  photo1  cat  -->
    
    	<div id="content" class="catphotos">
    
    	<?php $my_query = new WP_Query('category_name=photos1&p=4'); while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID; ?>
    
    		<div class="small1">
    
    			<h2>Photos 1</h2>
    
    			<div class="spaceit"></div>
    
    			<div id="post-<?php the_ID(); ?>">
    
    				<div class="entry">
    					<?php the_content(''); ?>
    
    					<?php edit_post_link('Edit', '<p>', '</p>'); ?>
    				</div>
    
    			</div>
    		</div>
    
    		<?php endwhile; ?>
    
     		<div class="small4"> 
    
    <?php if (is_category('photos1')) {query_posts ('category_name=photos1');} ?>
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
    
    			<div class="post" id="post-<?php the_ID(); ?>">
    
    				<div class="entry">
    					<?php the_content(''); ?>
    
    					<?php edit_post_link('Edit', '<p>', '</p>'); ?>
    				</div>
    
    			</div>
    		<div class="spaceit"></div>
    
    <?php
    $count++;
    $y = 4;
    $r = fmod($count, $y);
    if ($r==0)
    {
    echo '</div><div class="small4">';
    }
    ?>
    
    		 <?php endwhile; endif; ?>
    
    		</div>
    
    	</div>
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    Thread Starter baalinato

    (@baalinato)

    thanks a lot binh for ur interest.
    hi from montreal to vietnam.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘loops inside the loop’ is closed to new replies.