最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Hide Author By-Line if After Certain Date

programmeradmin1浏览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>";
    
    }
}

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
  • 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
Add a comment  | 

1 Answer 1

Reset to default 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>";
    
    }
}
发布评论

评论列表(0)

  1. 暂无评论