4
the image is worth a thousand words. take a look at it.

you know how craigslist has posts organized by date ..ex

Tue 3

post links for tuesday

Wed 4

post links for wed

Thurs 5

post links for thur

I know wordpress posts are organized by date by default. take a look at this alt text any ideas? btw if this did not make sense let me know. tx

5
  • Should the date be oldest first or newest first? Your numbers are going down but your days are going up. Are you just looking to GROUP content by date? Like have a single header for each date?
    – MathSmath
    Jan 5, 2011 at 4:24
  • haha..sorry about that fixed.. umm newest first.. thanks
    – andrewk
    Jan 5, 2011 at 4:35
  • By default, the function the_date() only prints the post's date if it's the first post of that date in your view. So a standard loop showing only the_date(); the_title(); would essentially look like you've described. What else are you looking for? Jan 5, 2011 at 5:37
  • hey i included an image. hopefully it makes sense..
    – andrewk
    Jan 5, 2011 at 7:58
  • I believe goldenapple has the right answer.
    – ZaMoose
    Jan 18, 2011 at 22:26

4 Answers 4

9
+50

You may notice that I did more or less exactly this for Matt's site: http://ma.tt. Every set of posts is grouped by the day. The basic principle is to keep track of your day in the loop, then print the date and related stuff only when it changes.

Take a basic Loop:

if ( have_posts() ) : while ( have_posts() ) : the_post();
  the_title();
  the_content();
endwhile; endif;

This just prints the title and content (without any formatting or anything) for everything in the current Loop. Now, you want it to pop out a new date every time the date changes. So you need to keep track of when it changes. Like so:

$loopday = '';
if ( have_posts() ) : while ( have_posts() ) : the_post();
  if ($loopday !== get_the_time('D M j')) {
    $loopday = get_the_time('D M j');
    echo $loopday;
  }
  the_title();
  the_content();
endwhile; endif;

What this does is to store the date that you're wanting to output into a variable. Each pass through the loop, it gets it again and checks to see if it has changed. If it has not changed, then nothing happens. If it has changed, then it sets the variable with the new date string, outputs it, then moves on.

Obviously this is just an example. The specific details depend on how your existing loop works and how you want to output the information.

While it's true that the_date() does this by default, sometimes it's easier to do it yourself in this manner, for formatting reasons.

4
  • thanks. just what i needed. will give you the bounty in 18 hrs.
    – andrewk
    Jan 18, 2011 at 0:16
  • Otto: I think you're overcomplicating it. Calls to the_date() should suffice, no? codex.wordpress.org/Function_Reference/the_date
    – ZaMoose
    Jan 18, 2011 at 22:25
  • Yes, calling the_date() will do the trick, but it doesn't give you the flexibility you sometimes need. On ma.tt, for example, I use this sort of thing to determine the date change and to close the previous article element and start a new one, thus allowing each date to be styled into its own box.
    – Otto
    Jan 18, 2011 at 22:43
  • Hey man, good to see you here helping out; +1. Jan 23, 2011 at 12:29
1

As @goldenapples noted, if your template uses the the_date(); template tag, it'll do this all on its own, as the default. I remember being confused the first time I used WordPress and I couldn't figure out how to get it to stop doing this.

Have you tested already and this isn't the result you're getting? It may simply be a matter of changing the tags your template is using.

Cheers.

3
  • Despite working with WordPress for years professionally there are new things I find daily. Neat little tag. Jan 5, 2011 at 14:20
  • Also, you can use the is_new_day() condition to determine whether or not to print your date heading: codex.wordpress.org/Function_Reference/is_new_day Jan 5, 2011 at 17:20
  • I've never gotten is_new_day() to work properly. Think it's been broken for a while.
    – Otto
    Jan 17, 2011 at 21:42
1

UPDATE updated according to notes in comment here you go, put this instead of your loop

  if (have_posts()) : while (have_posts()) : the_post(); 
        if($day_check = ''){ $day_check = $post->date}
        if ($day_check = $post->date){
            if (!$day_echod){
                echo '<div class="date">'.the_date().'</div>';
                $day_echod = true;
            }
        }else{
            $day_check = the_date();
            echo '<div class="date">'.the_date().'</div>';
        }
            ?>
            <div class="title_link">
                <a href="<?php the_permalink(); ?>"><?php the_title() ;?></a>
            </div>
            <?php           

               <?php endwhile; ?>
     <?php endif; ?>
1
  • The concept is right but the function the_date() echoes the date, rather than returning it. You'd have to use get_the_date or get_the_time in order to be able to use the result in the way you're trying to do. Also, use periods/full stops (.)for concatenating strings, rather than commas (,). Jan 18, 2011 at 0:36
0

get_the_date() or get_the_time() calls are overcomplicating the results. Simply use the_date(), which will only print the date once per day of posts by default.

See The Codex article on this subject:

http://codex.wordpress.org/Function_Reference/the_date

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.