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.

  • 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 at 7:03
  • I don't know where to start – Pete Dec 7 at 12:37

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 at 12:31
up vote 0 down vote accepted

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.';
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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