admin管理员组文章数量:1208155
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>";
}
}
本文标签: functionsHide Author ByLine if After Certain Date
版权声明:本文标题:functions - Hide Author By-Line if After Certain Date 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738710156a2108148.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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