WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Remove First Month

[Resolved] Remove First Month

  • Hello fellow WordPress forum members!

    I have a PHP script I’m using to display a list of all posts in an archive, and dividing the list into months, but I would really like to know how can I remove the first month from rendering with PHP. Here is my current code:

    <?php
    $prev_date = NULL;
    $date = NULL;
    ?>
    <?php query_posts("posts_per_page=-1&order=DESC"); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php
    $date  = get_the_time('m.Y');
    if($date != $prev_date)
    {
    $prev_date = $date;
    echo "<li class='month'>";
    echo get_the_time('F');
    echo "</li>";
    }
    ?>

    At this moment, and for the time being, I’m using CSS3 to hide the first child element with a class of “month”, since individual months get a class of “month”, but I would like to remove the markup all together.

    Thanks in advanced!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Since you didn’t really explain what first month means, check the Time Parameters section of the query posts article.

    query_posts()

    this could work:

    if($date != $prev_date)
    {
      if($prev_date != NULL)
      {
      echo "<li class='month'>";
      echo get_the_time('F');
      echo "</li>";
      }
    $prev_date = $date;
    }

    alchymyth,

    I don’t know how that works, but it works! haha. Thanks a lot!

    you want to know?

    – when you start off, the $prev_date is set to NULL;
    – you get the first result from the query;
    – you go to the first if statement for the first time; and the result is obviously that the $date is already set to something, while $prev_date is still NULL;
    you go to the second if statement for the first time; and $prev_date is still NULL, so the if is false and does not do the thing in the brackets (i.e. it is skipping your first date);
    – then you set $prev_date equal to $date;
    – next round, $prev_date is not NULL anymore …

    Interesting…thanks again alchymyth

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] Remove First Month’ is closed to new replies.
Skip to toolbar