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>";
}
}
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>";
}
}
Share
Improve this question
asked Feb 22, 2022 at 19:03
chuckscogginschuckscoggins
31 bronze badge
1
|
1 Answer
Reset to default 0It'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>";
}
}
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