我有一个XML提要url,现在我正在使用此代码从我的函数中的rss提要创建新的自定义帖子类型。php文件:
/*
| -------------------------------------------------------------------
| Schedule and update fashion news with the news rss feed
| -------------------------------------------------------------------
|
| */
if (!wp_next_scheduled(\'update_feed\'))
wp_schedule_event(current_time(\'timestamp\'), \'hourly\', \'update_feed\');
add_action(\'update_feed\', \'update_fashion_news\');
function update_fashion_news() {
// retrieve the previous date from database
$time = get_option(\'latestpostdate\');
//read the feed
if(function_exists(\'fetch_feed\')){
$uri = \'http://www.sitename.com/feed.xml\';
$feed = fetch_feed($uri);
}
if($feed) {
foreach ($feed->get_items() as $item){
$titlepost = $item->get_title();
$content = $item->get_content();
$description = $item->get_description();
$itemdate = $item->get_date();
$media_group = $item->get_item_tags(\'\', \'enclosure\');
$img = $media_group[0][\'attribs\'][\'\'][\'url\'];
$width = $media_group[0][\'attribs\'][\'\'][\'width\'];
// $latestItemDate = $feed->get_item()->get_date();
// if the date is < than the date we have in database, get out of the loop
if( $itemdate <= $time) break;
// prepare values for inserting
$post_information = array(
\'post_title\' => $titlepost,
\'post_content\' => $description,
\'post_type\' => \'fashionnews\',
\'post_status\' => \'publish\',
\'post_date\' => date(\'Y-m-d H:i:s\')
);
wp_insert_post( $post_information );
}
}
// update the new date in database to the date of the first item in the loop
update_option( \'latestpostdate\', $feed->get_item()->get_date() );
}
[更新:已更新上面的代码,以从simple\\u xml移动到使用simplepie][更新2:按照下面@Mridul的建议,移动代码并将其包装在WP计划事件中]
我仍在检查我的代码在下一批新闻更新到达时是否有效。有人认为这个代码会因为某种原因而不起作用吗?