0

I'm trying to find a way how to have a simple conditional whereby I can echo something depending on whether the post date is in the first half or last half of the post date year.

closed as off-topic by Krzysiek Dróżdż, Nathan Johnson, Rick Hellewell, rudtek, cjbj Feb 11 at 8:29

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • And what have you tried so far? See wordpress.stackexchange.com/help/how-to-ask. You're basically looking at extracting the MM out of YYYYMMDD, convert to int, and check if greater than 6. – anmari Dec 7 '18 at 7:03
  • I don't know where to start – Pete Dec 7 '18 at 12:37
2

Are you thinking of something like this?

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post(); 
        if ( absint( get_the_date( 'n' ) ) <= 6 ) {
            // First half of year
            ...
        } else {
            // Second half of year
            ...
        }
    }
}
  • that displays my post an infinite amount of times – Pete Dec 7 '18 at 12:31
  • No it wouldn't, unless you copied the whole code into an existing loop. – Jacob Peattie Feb 2 at 3:45
0

I found this elsewhere and it worked for me...

if( (int)get_the_time( 'm' ) <= 6 ) {
    echo 'Some time between January and June.';
} else {
    echo 'Some time between July and December.';
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.