Forums

php - tag help - post previous month (4 posts)

  1. tsalagi_red
    Member
    Posted 6 years ago #

    Hi,

    I have the front page of my site set to show only the current month. I have used this code:


    <?php if (is_home()) { ?>
    <?php $current_month = date('m'); ?>
    <?php $current_year = date('Y'); ?>
    <?php query_posts("monthnum=$current_month&year=$current_year") ?>

    What I want to do is also show the previous month's posts also. Anyone have an idea if this is possible, and how I might approach it? Can I use arithmatic operators in my parameters? for instance.

    Thanks.

  2. Kafkaesqui
    Moderator
    Posted 6 years ago #

    "Anyone have an idea if this is possible,"

    Not within a single query_posts instance. However, you could set up a second loop for this after the initial one.

    Also, if you're just trying to show posts for the previous month when no posts exist for the current one, this may be what you're after:

    http://wordpress.org/support/topic/43758#post-245469

  3. tsalagi_red
    Member
    Posted 6 years ago #

    Also, if you're just trying to show posts for the previous month when no posts exist for the current one, this may be what you're after:

    http://wordpress.org/support/topic/43758#post-245469

    Yes, this is what I am looking for (the second example), but I can't get it to work. I pasted your code between my if (have_posts) and while (have_posts) statements. Is this correct?

    It is behaving just like my original code. I have only one artlicle, and if I edit the timestamp to the previous month, I get a blank page.

  4. Kafkaesqui
    Moderator
    Posted 6 years ago #

    The entire bit of code (the $latest and query_posts lines) should be inserted before The Loop begins, as query_posts() needs to 'initialize' it. So before the if (have_posts) occurs.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags