2013-04-01 24 views

回答

1

您可以使用WordPress Transients APIWP_Query class

$random_id = ''; 
if (false === ($random_id = get_transient('some_random_post_id'))) { 
    // The transient expired, so create another 
    $args = array(
     'posts_per_page' => 1, #return one value 
     'orderby'  => 'rand', 
     'post_type'  => 'yourposttype' 
     ); 
    $single_post_query = new WP_Query($args); 
    while($single_post_query->have_posts()){ 
     $single_post_query->the_post(); 
     $random_id = get_the_ID(); 
     set_transient('some_random_post_id', $random_id, 60*60*24); # save the ID returned 
    } 

} 
//do your stuff with $random_id as the post id. 

请注意,它可能不是完全24小时,因为当有人访问您的网站时,WordPress会更新其暂态。另外,如果您使用WordPress 3.5或更高版本,则可以使用常量DAY_IN_SECONDS而不是60*60*24

+0

嗨RRikesh,感谢您的回答,我将如何做到这一点与自定义帖子类型?我也需要替换some_random_post_id?或者按原样离开?谢谢 –

+0

你可以改变它到你会记得的东西。对于自定义帖子类型,您需要在'$ args'中传递'post_type'参数。我正在更新我的答案。 – RRikesh

相关问题