RSS提要以固定顺序(从最新到最旧)包含所有项目。在这种情况下,您可以保存日期(&A);您上次创建帖子的时间(可选)(&;再次阅读提要时,可以查看以前保存的时间,以了解提要中的哪些帖子是新的&;插入它们,然后再次更新时间
您感兴趣的功能有update_option
, get_option
&;wp_insert_post
. 你可以在wordpress codex中找到它们的参考资料,只需谷歌一下。水流会是这样的
// retrieve the previous date from database
$time = get_option(\'mylastfeeddate\');
// include the code to read the xml file here &
// then the foreach loop as you did in the question
foreach($items as $item) {
// if the date is < than the date we have in database, get out of the loop
if( $item->pubDate < $time) break;
// assign the values in the format of wp_insert_post()
$out = array();
// insert the post
$post_id = wp_insert_post( $out );
}
// update the new date in database to the date of the first item in the loop
update_option( \'mylastfeeddate\', $items[0]->pubDate );
UPDATE
要使代码在某个固定时间段后执行,请使用wordpress cron函数,如下所示
if (!wp_next_scheduled(\'update_feed\'))
wp_schedule_event(current_time(\'timestamp\'), \'hourly\', \'update_feed\');
add_action(\'update_feed\', \'function_name\');
function function_name() {
// here goes all the code to read the feed
}
更改
hourly
你喜欢这里的时间是抄本参考
http://codex.wordpress.org/Function_Reference/wp_schedule_event用于添加自定义时间范围
add_filter(\'cron_schedules\', \'new_cron_schedules\');
function new_cron_schedules($schedules) {
array_merge($schedules, array(\'two_hourly\' => array( \'interval\' => 7200, \'display\' => __(\'Twice Hourly\') )));
}
在此过滤器之后,例如,您可以将上面的“hourly”替换为“two\\u hourly”