WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to display current month's posts only? (6 posts)

  1. deko
    Member
    Posted 7 years ago #

    In my index.php, I'd like to display only the current month's posts, and if there are no posts in the current month, then keep going back a month until some posts are found.

    Here's a first crack at it:

    This code goes in index.php just before the Loop starts:

    $m = date('m');
    $y = date('Y');
    query_posts("monthnum=".$m."&"."year=".$y);
    while (!have_posts())
    {
    $m = $m-1;
    if ($m == 0)
    {
    $m = 12;
    $y = $y - 1;
    }
    query_posts("monthnum=".$m."&"."year=".$y);
    }

    Any comments/suggestions welcome!

  2. lellie
    Member
    Posted 7 years ago #

    I know this is an age old problem for a lot of people.. I want the same thing but for a week, and I've not found a solution yet.

    I'll mention it here - but if people think I should start a new thread I will.

    I run a magazine using wordpress - I currently use 1 day to show weekly posts, because we update once a week - however I really want to be able to update a few times over the week - but still have exactly 1 week's (in calendar terms - not 7 days where there have been posts)

    If there is no posts in a week - I want the previous week in calendar terms to show up. If it helps I post all posts into an issue subcategory (of the issue date) which also represents a week's issue.

  3. deko
    Member
    Posted 7 years ago #

    current week might be tricky... not sure if query_posts takes such an argument. But with a little research and some PHP code, you could do it. I've got enough on my hands at the moment... sorry I cannot offer an example.

  4. ZMAng
    Member
    Posted 7 years ago #

    You might want to refer to an existing support thread on this issue.

  5. deko
    Member
    Posted 7 years ago #

    I had looked at that thread, but it was not very clear...

    The code I posted seems to be working, however...

  6. martytdx
    Member
    Posted 7 years ago #

    I borrowed this code, and I works well, but unfortunately, the code I have to offer up the "See More" (previous month) link isn't working.

    UPDATE: Figured it out.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags