0

我正在尝试创建一个插件来比较一条信息的时间。在 WordPress 数据库中到当前时间。例如,如果帖子在 5 月 6 日下午 12:00 过期,则在此之前它不会显示过期标签。

日期和时间表达式采用以下格式:MM/DD/YYYY HH:MM(军用时间)。到目前为止,这是我所拥有的 PHP 代码。有人可以查看我的代码,看看我是否做错了什么?请?:)

global $post;
$expired_post = get_post_meta( $post->ID, 'deal_exp_dt', true);
$expired_post_time = strtotime($expired_post);

// CONTENT FILTER
add_filter( 'the_content', 'my_the_content_filter', 20 );
function my_the_content_filter( $content ) {
if(time() > $expired_post_time && !empty($expired_post)) {
    $content = sprintf(
        '<div id="expired_deal_post"><strong><span style="font-size: x-large;">EXPIRED</span></strong><br><span style="font-size: medium;">This deal has ended.</span></div>%s',
        $content
    );

// Returns the content.
return $content;
}}
4

1 回答 1

1
function my_the_content_filter( $content ) {

// these two variables should reside this function scope, otherwise declare them as global

$expired_post = get_post_meta( $post->ID, 'deal_exp_dt', true);
$expired_post_time = strtotime($expired_post);



if(time() > $expired_post_time && !empty($expired_post)) {
    $content = sprintf(
        '<div id="expired_deal_post"><strong><span style="font-size: x-large;">EXPIRED</span></strong><br><span style="font-size: medium;">This deal has ended.</span></div>%s',
        $content
    );
 }

// Returns the content.
return $content;
}
于 2012-05-06T04:48:46.247 回答