0

I'm trying to use functions.php to hide the by-line on posts after a certain date. I'm using the following code but it isn't working. I think it's due to not properly grabbing the post date. How do I get the post date in functions.php?

// Hide By-Line

add_action('wp_head','my_head_css');
function my_head_css(){

$excludeDate = date("2021-06-02");
$postDate = get_the_date('Y-m-d');

if($postDate > $excludeDate){

    echo "<style> .entry-author {display:none !important;} </style>";
    
    }
}
1
  • What does var_dump( $postDate ); tell you? That will let you know if, at least, you've got a valid date string.
    – Pat J
    Commented Feb 22, 2022 at 19:19

1 Answer 1

0

It's probably getting the date as you expect, but the > is essentially a mathematical operator and you're trying to use it to compare two strings.

You can use strtotime() to convert the dates to integers (# of seconds elapsed since 1970-01-01), and compare those.

// Hide By-Line

add_action('wp_head','my_head_css');
function my_head_css(){

$excludeDate = date("2021-06-02");
$postDate = get_the_date('Y-m-d');

if( strtotime( $postDate ) > strtotime( $excludeDate ) ){

    echo "<style> .entry-author {display:none !important;} </style>";
    
    }
}
1
  • Hmm, having quickly read up on PHP operators, the > operator should be able to compare two date strings.
    – Pat J
    Commented Feb 22, 2022 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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