WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Remove First Month (6 posts)

  1. fear26700
    Member
    Posted 4 years ago #

    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!

  2. MichaelH
    Member
    Posted 4 years ago #

    Since you didn't really explain what first month means, check the Time Parameters section of the query posts article.

    query_posts()

  3. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    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;
    }
  4. fear26700
    Member
    Posted 4 years ago #

    alchymyth,

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

  5. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    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 ...

  6. fear26700
    Member
    Posted 4 years ago #

    Interesting...thanks again alchymyth

Topic Closed

This topic has been closed to new replies.

About this Topic