gpt4 book ai didi

php - 在 Wordpress 中向 get_the_date 添加一天

转载 作者:太空宇宙 更新时间:2023-11-03 12:03:14 26 4
gpt4 key购买 nike

在 WPTouch 主题中,发布日期显示在一个大气泡中。然而,帖子是前一天晚上添加的,所以这一天晚了一天。为了解决这个问题,我更改了这一行:

<?php wptouch_the_time( 'j' ); ?>

为此:

    <?php
$date = DateTime::createFromFormat('m-d-Y', mysql2date('m-d-Y', $post->post_date));
$date->modify('+1 day');
echo $date->format('j');
?>

这可行,但非常丑陋。我认为应该有更好的方法从 mysql 日期到 php DateTime。

最佳答案

您可以使用这个 filter在使用函数 get_the_date()(wptouch_the_time() 最有可能用来获取日期)将日期提供给主题之前修改日期:

add_filter( 'get_the_date', 'modify_get_the_date', 10, 3 );
function modify_get_the_date( $value, $format, $post ) {
$date = new DateTime( $post->post_date );
$date->modify( "+1 day" );
if ( $format == "" )
$format = get_option( "date_format" );
return( $date->format( $format ) );
}

话虽这么说,与其在从数据库中读取日期时修改日期,不如在写入时只存储发布日期会更好。您可以通过安排第二天的帖子来做到这一点,而不是直接发布它们。

关于php - 在 Wordpress 中向 get_the_date 添加一天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27922070/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com