• Resolved Anonymous User 4116433

    (@anonymized-4116433)


    I’m trying to create an archive page that shows past posts from a certain category, sorted by month. This code works but the page appears to be running the loop 3 times instead of 1. See link

    <?php
    // Declare some helper vars
    $previous_year = $year = 0;
    $previous_month = $month = 0;
    $ul_open = false;
    // Get the posts
    $myposts = get_posts('cat=16&numberposts=-1&orderby=post_date&order=DESC');
    ?>
    
    <?php foreach($myposts as $post) : ?>	
    
    	<?php
    
    	// Setup the post variables
    	setup_postdata($post);
    
    	$year = mysql2date('Y', $post->post_date);
    	$month = mysql2date('n', $post->post_date);
    	$day = mysql2date('j', $post->post_date);
    
    	?>
    
    	<?php if($year != $previous_year || $month != $previous_month) : ?>
    
    		<?php if($ul_open == true) : ?>
    		</ul>
    		<?php endif; ?>
    
    		<h3><?php the_time('F Y'); ?></h3>
    
    		<ul class="newslist">
    
    		<?php $ul_open = true; ?>
    
    	<?php endif; ?>
    
    	<?php $previous_year = $year; $previous_month = $month; ?>
    
    	<li class="<?php echo c2c_get_custom('Link Type'); ?>" style="list-style-type:none"><a href="<?php echo c2c_get_custom('URL'); ?>"><?php the_title(); ?></a></li>
    
    <?php endforeach; ?>
    	</ul>

    I’ve tried resetting the loop before and after this piece of code with no luck. Any suggestions would be much appreciated!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi

    The loop is only running one time. The code you have posted creates a break after each month’s posts.

    What is the problem you have with what is being displayed?

    Thread Starter Anonymous User 4116433

    (@anonymized-4116433)

    Turns out the code above works just fine. The problem was that it was inside another loop within page.php.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘get_posts creating 3 loops instead of 1’ is closed to new replies.