• Resolved FRANTIC

    (@frantic)


    I am trying to find out the best way to display the latest posts from the day before.

    Currently, I am using the method shown in CODE1 (see below).

    So, here are my questions…

    1. Are there any other ways of accomplishing the same thing? Perhaps better than what I’m doing.

    2. Is there anything wrong with my current method? The code works fine right now, but does anyone see any possible errors it might encounter?

    Thanks.

    —– CODE1 ————————————

    <?php
    /* yesterday’s headlines */
    Function yesterdayHeadlines(){
    $current_day = date(‘d’)-1;
    $current_month = date(‘m’);
    $current_year = date(‘Y’);
    query_posts(“cat=-63&year=$current_year&monthnum=$current_month&day=$current_day&orderby=date&order=DESC”);
    ?>

    The Loop goes here

    <?php } ?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • 2. Well, what happens to $current_day (and $current_month as well) when it’s the 1st of the month? The math for calculating the previous calendrical day can be convoluted to work out when done this way. However, we can get PHP to provide this for us, so…

    1. Use this code to set the values for your ‘current’ variables.

    $yesterday = date('U') - 86400;
    $yesterday = getdate($yesterday);
    $current_day = zeroise($yesterday['mday'],2);
    $current_month = zeroise($yesterday['mon'],2);
    $current_year = $yesterday['year'];

    Reference on getdate(): https://php.net/getdate

    zeroise() is an internal WordPress function that is used to set a number to a fixed digit count; for those less than the specified amount — in this case 2 — it will prepend a zero to it.

    Thread Starter FRANTIC

    (@frantic)

    Thanks, Kafkaesqui.

    Boy, that code sure looks better than what I had.

    The info on getdate() and zeroise() comes in handy, too.

    EDIT: I guess we can call this one “resolved”.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Displaying posts from THE DAY BEFORE’ is closed to new replies.