Support » Fixing WordPress » How to display current month on index page

  • How do I just have current months display on the index page. I am sure it is a simple fix like “display current month” but right now I still show february so there is quite a bit of scrolling.

    Thanks in advance

Viewing 12 replies - 1 through 12 (of 12 total)
  • Dawg,
    Do you have any March content?

    Yes I have several march items but below the Feb kicks in.

    Either this has not been seen, is too easy, but no response?

    Here’s a quick PHP script to do what you want.

    Put this at the top of your index.php
    <?php $current_month = date('m'); ?>

    Put this just after your loop starts (while (have posts))…

    $post_month = get_the_time('m');
    if($post_month == $current_month) {
    -- insert regular post loop here --

    Not fancy but it will work.

    – It gets the current month once (it’s outside of the loop)
    – In the loop it gets the month the post was created
    – if the current month and the post created month are the same it displays the post
    – if they’re not it displays nothing

    This code is unforgiving. If it’s March 12th and you haven’t posted anything in March the front page content section will be blank.

    Thanks Ming, I trust it will work my server seems slow so I will add it later.

    $post_month = get_the_time(‘m’);
    if($post_month == $current_month) {
    — insert regular post loop here —
    ?> This is the part I am having trouble get right because I do not know exactly where to implement in this line of code, tried four times each time with an error

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?> If you could show me the combined I would greatly appreciate it, I know I am still learning.

    Sorry for the lengthy delay. I didn’t like the last hack (we’re better than hacks!) so I was checking out the new 1.5 query vars.

    Check out this hotness:

    This goes anywhere before your loop starts
    <?php $current_month = date(‘m’); ?>
    <?php $current_year = date(‘Y’); ?>
    <?php query_posts(“monthnum=$current_month&year=$current_year”) ?>

    The old way used your regular query and then hid everything that didn’t match the current month. This new way rewrites the query so it only finds posts in the current month of the current year. If no posts are found you get your message saying ‘No posts found’ (or whatever it says in your index.php)

    Sweet the last worked like a charm. I will just make sure that at the beginning of month that I have some posts. Thanks again.

    Maybe I spoke too soon, it works perfect for displaying current month but when I click the archive links, I do not get prior months, it stay in the current month Hmm???

    Sorry, I should clarify myself. Put the lines anywhere above the post loop in the index.php file. That’s important because if you put the lines in the header.php then every page that calls the header will get that query – not what you want. If you restrict those lines to index.php then only that page will use the special query.

    But you make a good point that I didn’t address. Besides being the home page, index.php can also be used as a fallback for missing pages and as a default template in some themes. So we really need to make sure the query is only called if it’s on the home page. We use the is_home() function like this:

    <?php if (is_home()) { ?>
    <?php $current_month = date(‘m’); ?>
    <?php $current_year = date(‘Y’); ?>
    <?php query_posts(“monthnum=$current_month&year=$current_year”) ?>
    <?php } ?>

    3rd times a charm! Did I finally get this right?

    A client just asked me today to implement a feature like this . This code is great!! I only have one small problem. After adding this to my theme’s index.php, the LINKS and META sidebar entries disappear. Any ideas?? Thanks for a beautiful, functional piece of code!

    JimmyDean: Check the template holding your sidebar code (probably sidebar.php) for a php line like so:


    It just sounds like something is testing if one is on an archive page (your query_posts causes WordPress to fake this). Some other possible conditional tags that may be responsible:


Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘How to display current month on index page’ is closed to new replies.