سؤال

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

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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.

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; ?>

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى wordpress.stackexchange
scroll top