• Hello! Had trouble searching for this because “1 Year Ago” gives unrelated results.

    On my sidebar, I display 3 Recent Posts from 1 Category (I call it the “Setlists” category, from here on) using this code:

    <li><h2>3 Recent Radio Setlists</h2>
    <ul>
    <?php
    $posts = get_posts('numberposts=3&category=2');
    foreach ($posts as $post) :
    setup_postdata($post);
    ?>
    <a href="<?php the_permalink(); ?>"><?php the_excerpt(); ?></a>
    <?php
    endforeach; ?>
    </ul></li>

    QUESTION HERE — NOW, below that, I want to display 2 Setlists From 1 Year Ago. Then, 2 Setlists From 2 Years Ago. Then, 2 Setlists From 3 Years Ago. And so on. (I wish to go 5 years back.)

    How can I do that?

    I suppose it would technically be, 2 Setlists From This Same Month But Minus 1 Year. It’s August now, so I want to call up 2 posts from Last August.

    Not sure if I need to restart The Loop for each of these calls (years). I have a guess this could be done by modifying this line:
    $posts = get_posts('numberposts=3&category=2');
    Somehow like,
    $posts = get_posts('numberposts=3&category=2&year=-1');'
    Perhaps?

Viewing 13 replies - 1 through 13 (of 13 total)
  • $year = date('Y');
    $month = date('m');
    // this year:
    $posts = get_posts('numberposts=3&category=2');
    // last year:
    $year--;
    get_posts("numberposts=3&category=2&month=$monthnum&year=$year");
    // two years ago
    $year--;
    get_posts("numberposts=3&category=2&month=$monthnum&year=$year");

    Thread Starter Dgold

    (@dgold)

    Thanks for the reply, I will try that. Your code for “Last year” and “2 years ago” looks identical to me (besides the //comment text). I don’t understand how it will return different results. I’ll test and get back to you.

    $year-- means “decrement the value of year by one”. So, we first set the $year variable to the current year. Display some posts. Then decrement the year by one, and show some posts. Repeat as needed.

    Thread Starter Dgold

    (@dgold)

    Thanks again. I appreciate the explanation of how this works, as well as the sample code.

    Thread Starter Dgold

    (@dgold)

    Skippy, I’m trying. I am messing up where to put this code. When I put exactly what you gave me, it displays the code itself on my page. I guess I am supposed to put
    <?php
    before the $year ?
    and maybe I should put
    <?php
    endforeach; ?>

    after what you gave me?

    Newbie here, I’m not sure if I am re-initiating the Loop again (one Loop for each year), or if this can all happen within my Recent Setlists Loop that I showed the code here in post #1, above. Apparently not, cuz when I put it within this Loop it simply displays the code itself, after each post.

    If you can please make it even more clear for me, where to insert this code and if you left out anything that you assumed I would know, like starting and ending the PHP?

    yes, it is php code, thus you must play <?php before his code and ?> after it.

    Thread Starter Dgold

    (@dgold)

    One time, or several times, like before every $ ?

    Now I have this (I put in those little PHP start and PHP end, before and after the entire chunk of code Skippy gave me), and it’s no-worky worky —

    <ul>
    <?php
    year = date('Y');
    $month = date('m');
    // this year:
    $posts = get_posts('numberposts=3&category=2');
    // last year:
    $year--;
    get_posts("numberposts=3&category=2&month=$monthnum&year=$year");
    // two years ago
    $year--;
    get_posts("numberposts=3&category=2&month=$monthnum&year=$year");
    ?>
    </ul>

    I know, I’m not too smooth with PHP. Trying to learn. Maybe I am supposed to start and stop the PHP around each block, 1 year ago, 2 years ago, etc.?


    <?php
    $year = date('Y');
    $month = date('m');

    // last year:
    $year--;
    $posts = get_posts("numberposts=3&category=2&month=$monthnum&year=$year");
    foreach ($posts as $post) :
    setup_postdata($post);
    ?>
    <a href="<?php the_permalink(); ?>"><?php the_excerpt(); ?></a>
    <?php
    endforeach; ?>


    <?php
    // two years ago
    $year--;
    $posts= get_posts("numberposts=3&category=2&month=$monthnum&year=$year");
    foreach ($posts as $post) :
    setup_postdata($post);
    ?>
    <a href="<?php the_permalink(); ?>"><?php the_excerpt(); ?></a>
    <?php
    endforeach; ?>

    You can open and close contiguous blocks of PHP just once. There’s no need to do it for each line.

    Try this. Completely untested.
    <li><h2>3 Recent Radio Setlists</h2>
    <ul>
    <?php
    $posts = get_posts('numberposts=3&category=2');
    foreach ($posts as $post) :
    setup_postdata($post);
    ?>
    <a href="<?php the_permalink(); ?>"><?php the_excerpt(); ?></a>
    <?php
    endforeach; ?>
    </ul></li>
    <?php
    $year = date('Y');
    $month = date('m');
    for ($x = 0; $x <= 2; $x++){
    $year--; ?>
    <li><h2>3 Radio Setlists from <?php echo $year ?></h2>
    <ul>
    <?php
    $posts = get_posts("numberposts=3&category=2&year=$year&monthnum=$month");
    foreach ($posts as $post) :
    setup_postdata($post);
    ?>
    <a href="<?php the_permalink(); ?>"><?php the_excerpt(); ?></a>
    <?php
    endforeach; ?>
    </ul></li>
    <?php
    } // end for loop of previous year's playlists
    ?>

    Thread Starter Dgold

    (@dgold)

    Now, I’m getting somewhere. Not quite there yet.

    Skippy’s latest code posted above results in this (basically): The headlines work with decrementing the year. But the post excerpts appearing under each headline, are identical: always the most-recent 3 setlists from 2005. As if the year did not decrement when calling the posts.

    Example of the results:

    3 Recent Radio Setlists

    7-26-2005
    SET ONE: Heroes > Junior > Party At….

    7-19-2005
    etc…

    3 Radio Setlists from 2004

    7-26-2005
    SET ONE: Heroes > Junior > Party At….

    7-19-2005
    etc…

    3 Radio Setlists from 2003

    7-26-2005
    SET ONE: Heroes > Junior > Party At….

    7-19-2005
    etc…

    etc.

    EDIT: As of now I only have 1 post in the database in 2004, 1 post in 2003, 0 posts in 2002 (I plan to import them soon)… so that could be the problem, although it’s not even showing the 1 post in 2003.

    I really appreciate everyone who’s taking the time to help me here.

    Thread Starter Dgold

    (@dgold)

    Aha! Reading Skippy’s latest code… It looks like he nicely has it referencing the month. It’s now August 2005, so Skippy’s code is looking for AUGUST 2004, and AUGUST 2003. And if there are no posts in August…. ? It’s displaying 2005. Could that be it?

    Can I rephrase the request… to do the 3 most recent posts from that year, whether the month matches or not?

    Try taking monthnum out of the get_posts() call.

    Thread Starter Dgold

    (@dgold)

    I tried that, Skippy, and got the same results using
    $posts = get_posts("numberposts=2&category=2&year=$year");
    I have not announced the page yet, but you can look at my test here https://dj.dgold.info/

    It’s interesting, because the
    $year--;
    part seems to work for the headline, but not for calling the posts under that headline. I would’ve thought, once it had the year decremented, it would stay that way for the remainder of that Loop.

    Any more ideas?

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘How to display 2 posts from 1 year ago, 2 posts from 2 years ago, etc.’ is closed to new replies.