WordPress.org

Ready to get started?Download WordPress

Forums

How to display current month on index page (13 posts)

  1. dawg
    Member
    Posted 9 years ago #

    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

  2. NuclearMoose
    Member
    Posted 9 years ago #

    Dawg,
    Do you have any March content?

  3. dawg
    Member
    Posted 9 years ago #

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

    http://cafe789.com/kyles_eyes

  4. dawg
    Member
    Posted 9 years ago #

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

  5. Ming
    Member
    Posted 9 years ago #

    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))...

    <?php
    $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.

  6. dawg
    Member
    Posted 9 years ago #

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

  7. dawg
    Member
    Posted 9 years ago #

    <?php
    $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.

  8. Ming
    Member
    Posted 9 years ago #

    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)

  9. dawg
    Member
    Posted 9 years ago #

    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.

  10. dawg
    Member
    Posted 9 years ago #

    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???

  11. Ming
    Member
    Posted 9 years ago #

    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?

  12. JimmyDean
    Member
    Posted 9 years ago #

    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!

  13. Kafkaesqui

    Posted 9 years ago #

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

    if(is_archive())

    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:

    is_date()
    is_year()
    is_month()
    is_day()
    is_time()

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.