• Okay here is some php code that displays posts grouped by the_date for use in a sidebar:

    <ul>
    <?php if (have_posts()) :
    while (have_posts()) : the_post();
    echo the_date('F j','<li>','</li>');
    echo "<li><a href=\"";
    echo the_permalink();
    echo "\">";
    echo the_title();
    echo " by ";
    echo the_author_posts_link();
    echo "</li>";
    endwhile;
    endif; ?>
    </ul>

    I want to find a way to nest the posts that appear for each use of the_date in a new <ul> tag but I can’t think through it. Can anybody assist? Thanks!!

    See it in use here: http://wp.sacrag.com on the top left (recent posts)

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    I’m uncertain as to what you’re trying to do. Are you saying that you want to do a new UL every time the_date changes? I really don’t see why you’d want to do that in this case, but hey, to each his own.

    the_date has a unique property, as you may have noticed. It won’t print the same thing twice in a row. If you look at your recent posts for Sept. 19th, you’ll see that the September 19th only printed once for two posts, even though the_date was called twice (think about it).

    You can use this property to your advantage here.

    <ul>
    <?php if (have_posts()) :
    while (have_posts()) : the_post();
    echo the_date('F j','</ul><ul><li>','</li>');
    echo "<li><a href=\"";
    echo the_permalink();
    echo "\">";
    echo the_title();
    echo " by ";
    echo the_author_posts_link();
    echo "</li>";
    endwhile;
    endif; ?>
    </ul>

    This is a bit unpleasant, I grant you, since it leaves a useless <ul></ul> at the top of the result. You could work around this by handling the changing date code yourself. For this, you’d need get_the_time(), like so:

    <ul>
    <?php if (have_posts()) :
    $start_loop = true;
    while (have_posts()) : the_post();
    $curr_date = get_the_time('F j');
    if (!$start_loop && $curr_date != $old_date) {
    echo "</ul><ul>";
    }
    $start_loop = false;
    $old_date = $curr_date;
    echo the_date('F j','<li>','</li>');
    echo "<li><a href=\"";
    echo the_permalink();
    echo "\">";
    echo the_title();
    echo " by ";
    echo the_author_posts_link();
    echo "</li>";
    endwhile;
    endif; ?>
    </ul>

    This one is slightly more complex. Basically, we ignore the first run through the loop using a boolean variable, then for the rest of the runs, if the date string changes, we close the UL and start a new one.

    Thread Starter donzacharias

    (@donzacharias)

    Thanks! That was too many <ul>s but I played around with it and changed
    if (!$start_loop && $curr_date != $old_date) {
    echo "
    </ul><ul>“;
    to
    if (!$start_loop && $curr_date != $old_date) {
    echo "
    </li></ul>“;

    and
    the_date('F j','<li>‘,’</li>‘);
    to
    the_date('F j','<li>‘,’<ul>‘);

    and got what I wanted.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    That don’t seem right. You’ve got one too many </li>‘s in there now. Your page doesn’t validate because of it.

    Thread Starter donzacharias

    (@donzacharias)

    I think you were right, but
    echo "</ul></li>“;
    after the if block works right and validates.

    Check it here in Recent Posts http://sacrag.com

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘nested list of posts using the_date’ is closed to new replies.