Forums

[resolved] How to check if a certain month has posts or not? (3 posts)

  1. ludder
    Member
    Posted 3 years ago #

    I'm working on my own theme. On a single page I want to show links to the archives of the previous and next months. However, if that month contains no posts, I don't want to link to it. How can I check of a certain month has posts or not?

    This code for example gives me a link to the previous month.

    get_month_link(get_the_time('Y'), get_the_time('m')-1)

    If that month contains no posts, the link redirects to a 404-page. I'd rather not show the link at all.

  2. MichaelH
    Volunteer
    Posted 3 years ago #

    What about using the template tag, wp_get_archives(), with the type=monthly argument?

    If necessary, look at the query_posts article for time parameters that would allow you to get posts for a given month:

    $month_posts=query_posts(year=20097monthnum=4');
    if ($month_posts) {
    //there's posts for april 2009
    }

  3. ludder
    Member
    Posted 3 years ago #

    Your second suggestion will help me out - thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic