Forums

[resolved] Limit front page post by date range (4 posts)

  1. slunk3
    Member
    Posted 4 years ago #

    Hello,

    Is there a way to limit the number of posts on the front page by a date range? As in, all posts for the last 30 days.

    Many thanks!

  2. MichaelH
    Volunteer
    Posted 4 years ago #

    Using the WordPress Default 1.6 theme index.php file:

    Changed this code from:
    <?php while (have_posts()) : the_post(); ?>

    to:

    <?php while (have_posts()) : the_post(); ?>
    <?php
    $mylimit=30*(60*60*24);
    if ( (current_time(timestamp) - get_the_time('U') - (get_settings('gmt_offset') * 3600) ) < $mylimit)
    {
    ?>

    and this code from:

    </div>
    
    <?php endwhile; ?>

    to:

    </div>
    <?php } ?>
    
    <?php endwhile; ?>

    Entire index.php posted at http://wordpress.pastebin.ca/746974

    Example to change the number of days from 30 to 45
    $mylimit=45*(60*60*24);

  3. slunk3
    Member
    Posted 4 years ago #

    Great! This works, but is there a way to still access older posts from the index page?

    using

    <?php posts_nav_link(' — ', __('&laquo; Previous Page'), __('Next Page &raquo;')); ?>

  4. jimmiejo
    Member
    Posted 4 years ago #

    This works brilliantly.

    I'm using it to display the posts from the past 24 hours.

    If there weren't any posts in the past 24 hours it obviously displays nothing.

    So my question is, is there a way to use that code and add to it so if no posts are available, it displays the previous day's?

    Maybe you could add to:

    $mylimit=1*(60*60*24);

    with:

    elseif $mylimit=2*(60*60*24);

    ?

Topic Closed

This topic has been closed to new replies.

About this Topic