2012-05-06 28 views
0

我试图创建一个插件来比较一段信息的时间。在WordPress数据库到当前时间。例如,如果该帖子在5/6在12:00 pm过期,则在该时间之前它不会显示过期的标签。用PHP和WordPress比较时间

日期和时间表达式采用以下格式: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; 
}} 
+0

和你有什么具体问题? – thecodeparadox

+1

您在函数外部有$ expired_post_time,这对典型的PHP安装不起作用。 –

回答

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; 
} 
+0

非常感谢!它正在工作:D – Mitchell