WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Archive page listed monthly - but I have one problem (3 posts)

  1. tida19
    Member
    Posted 1 year ago #

    According to this post
    http://wordpress.org/support/topic/display-archive-by-month-and-then-post-by-post

    I've been trying to show archive like:
    January 2012
    dd.mm.yy - post
    dd.mm.yy - post

    February 2012
    dd.mm.yy - post
    dd.mm.yy - post

    But it shows like always have one post of the current month and repeat current month below

    October 2012
    03.10.2012 - post

    October 2012
    02.10.2012 - post
    01.10.2012 - post

    here is the code please help me try to fix it

    <?php // Variables $month, $prevmonth, $year and $prevyear are used to section the archive display ?>
    <?php 	$month = '';
    		$prevmonth = '';
    		$year = '';
    		$prevyear = ''; ?>
    
    <?php // Cycle through all the posts to display the archive ?>
    <?php query_posts('showposts=-1'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    	<?php // Find the month/year of the current post ?>
    	<?php $year = mysql2date('Y', $post->post_date); ?>
    	<?php $month = mysql2date('F', $post->post_date); ?>
    
    	<?php // Compare the current month with the $prevmonth ?>
    	<?php if ($month != $prevmonth) { ?>
    		<?php // If it is different, display the new month and reset $prevmonth ?>
    		<br/><br/><h3><?php echo $month . ' ' . $year; ?></h3><br/>
    		<?php $prevmonth = $month; ?>
    
    	<?php // In case the previous post was a year ago in the same month ?>
    	<?php } elseif ($year != $prevyear) { ?>
    		<h3><?php echo $month . ' ' . $year; ?></h3>
    		<?php $prevmonth = $month; ?>
    		<?php $prevyear = $year; ?>
    	<?php } ?>
    
    	<?php // Display the post in this month ?>
    	<p ><a class="list_archive" href="<?php the_permalink(); ?>"><?php echo mysql2date('d.m.y', $post->post_date); ?> - <?php the_title(); ?></a></p>
    <?php endwhile; endif; ?>
    <br/>
  2. vtxyzzy
    Member
    Posted 1 year ago #

    I believe that you need to set $prevyear at the same time you set $prevmonth. Try changing this:

    <?php if ($month != $prevmonth) { ?>
    		<?php // If it is different, display the new month and reset $prevmonth ?>
    		<br/><br/><h3><?php echo $month . ' ' . $year; ?></h3><br/>
    		<?php $prevmonth = $month; ?>

    to this:

    <?php if ($month != $prevmonth) { ?>
    		<?php // If it is different, display the new month and reset $prevmonth ?>
    		<br/><br/><h3><?php echo $month . ' ' . $year; ?></h3><br/>
    		<?php $prevmonth = $month; ?>
    		<?php $prevyear = $year; ?>
  3. tida19
    Member
    Posted 1 year ago #

    All right! it works well :)

    Thank you so much I looked over it

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags