WordPress.org

Ready to get started?Download WordPress

Forums

show post dates, but list year only once? (4 posts)

  1. gmisen
    Member
    Posted 5 years ago #

    Hi, this has been driving me crazy, maybe someone here has done this before?

    I want to list my posts and show the date. But I only want the year to show in the top post of that year. The latest post in 2008 would read December 2008, but the following ones would read only July, March, January --- No year. Then December 2007, followed by only July, March, etc etc.

    I've tried a bunch of ways to accomplish this, but nothing seems to work. Any suggestions?

    Thanks!

  2. MichaelH
    Member
    Posted 5 years ago #

    Try this. Example is a loop so you may have to wedge this into your existing code:

    <?php
    $year='';
    while (have_posts()) : the_post();
    if (mysql2date("Y", $post->post_date) != $year) {
    $year = mysql2date("Y", $post->post_date);
    $count=1;
    }
    the_title();
    if ($count == 1) {
    the_time('F Y');
    } else {
    the_time('F');
    }
    $count++;
    endwhile;
    ?>

    Related:
    the_time()

  3. gmisen
    Member
    Posted 5 years ago #

    woah thanks! that worked nicely

  4. gmisen
    Member
    Posted 5 years ago #

    Actually, I take that back. On my computer it looks fine. On my coworker's PC the post content does not show up (or on my mac when i use my Firebug firefoxplugin on the page).
    The dates show up but the content won't. I was hoping this wouldn't be too complicated for a browser to handle. (this is the first of three loops on a page)

    Can anyone tell what I'm doing wrong?

    <div class="hr"><hr /></div>
    <a name="timeline"></a>
    
    <div class="presspost" valign="top" id="post-<?php the_ID(); ?>">
    <div class="pressentry">
    
    <p class="about_caption2"><a name="Timeline"></a>Company History</p>
    <table class="timeline" border="0">
    <tbody>
    
    <?php $my_query = new WP_Query('category_name=timeline&showposts');
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    <tr>
    <td class="namendate3" width="34">
    
    <?php if (mysql2date("Y", $post->post_date) != $year) {
    $year = mysql2date("Y", $post->post_date);
    $count=1;
    } if ($count == 1) {echo "<br />"; the_time('Y');} $count++; ?>
    
    </td>
    
    <tr>
    <td class="namendate2">
    
    <?php the_time('M') ?>
    
    </td>
    
    <td>
    
    <?php the_content('<p class="serif">Read the rest of this page &raquo;</p>'); ?>
    
    <?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
    
    </td>
    </tr>
    
    <?php endwhile; ?>
    
    </tbody>
    </table>
    
    <br />
    <br />
    
    <p align="right"><a href="#top">back to top</a></p>

Topic Closed

This topic has been closed to new replies.

About this Topic