WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to display all posts on monthly archives? (5 posts)

  1. DailyBlogTips
    Member
    Posted 1 year ago #

    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?

  2. bcworkz
    Member
    Posted 1 year ago #

    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.

  3. DailyBlogTips
    Member
    Posted 1 year ago #

    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.

  4. DailyBlogTips
    Member
    Posted 1 year ago #

    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" );
  5. bcworkz
    Member
    Posted 1 year ago #

    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 :/

Topic Closed

This topic has been closed to new replies.

About this Topic