法国小型影院WordPress对接XML排片自动展示方案求助
影院排片XML与WordPress自动化对接方案
方案一:调整WP All Import + ACF配置(解决你之前的失败问题)
你之前用这两个插件没成功,大概率是没设置分组导入规则,按以下步骤调整:
- 先在ACF里给影片自定义文章类型(比如
movie)创建重复字段组,命名为showtimes,包含日期、时间、厅号等子字段。 - 打开WP All Import,选择XML数据源后,进入"导入设置",在"记录识别"环节,选择按影片唯一标识(比如XML里的
FilmID)作为匹配键,而不是按单条排片记录导入。 - 字段映射时,找到ACF的
showtimes重复字段,将XML中同一影片的所有排片条目(日期、时间等)映射进去,开启WP All Import的合并相同条目功能,确保同一影片的排片会被合并到同一个页面的重复字段中。 - 设置更新规则:勾选"更新现有条目",并添加过滤条件,自动删除XML中不存在的过期排片(比如设置日期小于当前日期的场次自动移除)。
方案二:自定义代码实现(更灵活可控)
如果插件配置还是有问题,用自定义代码可以完全掌控流程,步骤如下:
1. 准备基础结构
- 在WordPress中创建自定义文章类型
movie(可以用代码或Custom Post Type UI插件),用于存储影片信息。 - 用ACF或自定义字段给
movie类型添加film_id(存储XML中的影片唯一ID)和showtimes(存储排片数组)字段。
2. 编写同步脚本
创建一个PHP函数,负责拉取、解析XML并同步到WordPress:
function sync_cinema_showtimes() { // 拉取XML数据 $xml_url = 'https://cdsdemorc1.cineoffice.fr/TMSexport/cdsdemorc1'; $xml = simplexml_load_file($xml_url); if (!$xml) { error_log('影院排片XML加载失败'); return; } // 按影片ID分组排片数据 $movies_data = []; $current_date = date('Y-m-d'); // 过滤过期场次用 foreach ($xml->Seance as $seance) { $film_id = (string)$seance->FilmID; $show_date = (string)$seance->Date; // 跳过已过期的排片 if ($show_date < $current_date) continue; if (!isset($movies_data[$film_id])) { $movies_data[$film_id] = [ 'title' => (string)$seance->Titre, 'showtimes' => [] ]; } $movies_data[$film_id]['showtimes'][] = [ 'date' => $show_date, 'time' => (string)$seance->Heure, 'room' => (string)$seance->Salle ]; } // 同步到WordPress foreach ($movies_data as $film_id => $data) { // 查找已存在的影片文章 $existing_posts = get_posts([ 'post_type' => 'movie', 'meta_key' => 'film_id', 'meta_value' => $film_id, 'posts_per_page' => 1, 'post_status' => 'publish' ]); if (!empty($existing_posts)) { // 更新现有影片的排片和标题 $post_id = $existing_posts[0]->ID; wp_update_post([ 'ID' => $post_id, 'post_title' => $data['title'] ]); update_post_meta($post_id, 'showtimes', $data['showtimes']); } else { // 创建新的影片文章 $post_id = wp_insert_post([ 'post_title' => $data['title'], 'post_type' => 'movie', 'post_status' => 'publish' ]); add_post_meta($post_id, 'film_id', $film_id); add_post_meta($post_id, 'showtimes', $data['showtimes']); } } }
3. 设置自动同步任务
把脚本设置为每周自动执行,在主题的functions.php或自定义插件中添加:
// 每周一凌晨2点执行同步(可根据排片更新时间调整) if (!wp_next_scheduled('cinema_showtimes_sync_hook')) { wp_schedule_event(strtotime('next Monday 02:00'), 'weekly', 'cinema_showtimes_sync_hook'); } add_action('cinema_showtimes_sync_hook', 'sync_cinema_showtimes');
4. 前端展示排片
在影片的自定义模板(single-movie.php)中添加代码,输出排片信息:
<?php $showtimes = get_post_meta(get_the_ID(), 'showtimes', true); if (!empty($showtimes)) : ?> <section class="movie-showtimes"> <h3>排期安排</h3> <ul> <?php foreach ($showtimes as $show) : ?> <li> <?php echo esc_html($show['date']); ?> - <?php echo esc_html($show['time']); ?> | 厅<?php echo esc_html($show['room']); ?> </li> <?php endforeach; ?> </ul> </section> <?php endif; ?>
注意事项
- 确保WordPress服务器有权限拉取外部XML文件(检查防火墙或安全插件设置)。
- 测试同步脚本时,可以手动执行一次(比如添加AJAX钩子触发),确认数据是否正确同步。
- 定期检查错误日志,排查同步失败的问题。
内容的提问来源于stack exchange,提问作者Duarte
相关产品推荐
相关产品推荐

