Support » Plugins » Hacks » How to display all posts on monthly archives?

  • Resolved DailyBlogTips


    On both category and monthly archive pages I am trying to display all posts on a single page (instead of 10 per page). On category pages I already managed this with the following code:

    $cat_id = get_cat_ID( single_cat_title(null, false) );
    query_posts( "cat=$cat_id&posts_per_page=-1" );

    I believe on monthly archives I can achieve the same thing with this:

    $year = ???
    $monthnum = ???
    query_posts( "year=$year&monthnum=$monthnum&posts_per_page=-1" );

    However, I don’t know how to get the $year and $monthnum variables, as you can see above. I searched on WordPress codex, but it only shows how to get the current year and month, while what I need is the year/month of the archive page being displayed at the moment.

    Any help?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz


    Try get_the_date('Y') for year. Use ‘m’ for month number. The Codex documentation says this is for the current post in the loop, but twentyeleven theme uses this tag outside the loop to get the date of the archive it is displaying.

    get_the_date(‘Y’) worked perfectly for year. get_the_date(‘M’) didn’t work for month though. So right now when I load the archives of a single month it loads all the posts of that year.

    I am investigating, and thanks for the help.

    get_the_date(‘n’) solved the problem. I guess the query_posts function receives the month parameter without leading zeroes, so ‘M’ won’t work as it adds leading zeroes.

    My code ended up like this:

    $year = get_the_date('Y');
    $monthnum = get_the_date('n');
    query_posts( "year=$year&monthnum=$monthnum&posts_per_page=-1" );
    Moderator bcworkz


    Glad you worked that out! Sorry for the misdirection about the ‘M’ bit 🙁

    I really should test things before I post. I could say it’s so I can help more people, but really, I’m just lazy :/

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to display all posts on monthly archives?’ is closed to new replies.