我正在尝试在wordpress和自定义帖子类型中编写日历脚本。到目前为止工作正常。我使用几个自定义字段(ACF)调用所需的参数(事件标题,开始日期,结束日期等)
我用get_posts()
叫结果。
但是我在数组中命名键存在问题。在foreach操作之后,我可以获得所需的值,例如通过键入event_title
或$posts[0]
之前的开始日期$posts['1']
。
如何实现我可以命名键的名称,以便可以在输出中使用$posts[title]
或$posts['start_date']
?
我尝试使用array_combine构建第二个数组,但是失败了。
<?php
$args = array( 'post_type' => 'cpt_kalender', 'post_status' => 'publish', 'numberposts' => '-1', 'meta_key' => 'cf_termin_start', 'orderby' => 'meta_value_num', 'order' => 'ASC' );
$posts = get_posts( $args );
$group_posts = array();
foreach ( $posts as $post ) : setup_postdata( $post );
/*
Hole die Termine der einzelnen Startdaten
- Schreibe das Datumsformat yyyymmdd in yyyy-mm-dd um
*/
$titel = get_the_title();
$termin_datum_start = get_field('cf_termin_start');
$termin_datum_start = new DateTime($termin_datum_start);
$termin_datum_ende = get_field('cf_termin_ende');
$termin_datum_ende = new DateTime($termin_datum_ende);
$termin_jahr = $termin_datum_start->format('Y');
$termin_monat = $termin_datum_start->format('F');
$termin_tag = $termin_datum_start->format('d');
$post_id = get_the_ID();
$termin_start_uhrzeit_stunde = get_field('cf_termin_start_uhrzeit_stunde');
$termin_start_uhrzeit_minute = get_field('cf_termin_start_uhrzeit_minute');
$termin_ende_uhrzeit_stunde = get_field('cf_termin_ende_uhrzeit_stunde');
$termin_ende_uhrzeit_minute = get_field('cf_termin_ende_uhrzeit_minute');
$termin_schulfrei = get_field('cf_termin_schulfrei');
$termin_intern = get_field('cf_termin_intern');
/*
Fülle das Array mit den Werten
*/
$group_posts[$termin_jahr][$termin_monat][] = array(
$titel,
$termin_datum_start,
$termin_datum_ende,
$post_id,
$termin_start_uhrzeit_stunde,
$termin_start_uhrzeit_minute,
$termin_ende_uhrzeit_stunde,
$termin_ende_uhrzeit_minute,
$termin_schulfrei,
$termin_intern
);
endforeach; ?>
有没有办法在相同的foreach操作中告诉上面列出的数组所需的键名?
我很感谢每一个提示。多谢。我只是在学习php,这使我发疯。 :(
答案 0 :(得分:0)
将值添加到$group_posts
数组的部分。您可以为每个项目添加键。
$group_posts[$termin_jahr][$termin_monat][] = array(
'title' => $titel,
'termin_datum_start' => $termin_datum_start,
'termin_datum_ende' => $termin_datum_ende,
'post_id' => $post_id,
'termin_start_uhrzeit_stunde' => $termin_start_uhrzeit_stunde,
'termin_start_uhrzeit_minute' => $termin_start_uhrzeit_minute,
'termin_ende_uhrzeit_stunde' => $termin_ende_uhrzeit_stunde,
'termin_ende_uhrzeit_minute' => $termin_ende_uhrzeit_minute,
'termin_schulfrei' => $termin_schulfrei,
'termin_intern' => $termin_intern
);
然后,您可以访问类似$group_posts['termin_jahr']['termin_monat']['title']
的值
由于要为每个项目添加termin_jahr
和termin_monat
,因此您需要先提供这些内容才能访问正确的项目标题。
或者,您可以仅将这些变量添加到数组内部,以后再访问它,如下所示:
$group_posts = array(
'title' => $titel,
'termin_datum_start' => $termin_datum_start,
'termin_datum_ende' => $termin_datum_ende,
'post_id' => $post_id,
'termin_start_uhrzeit_stunde' => $termin_start_uhrzeit_stunde,
'termin_start_uhrzeit_minute' => $termin_start_uhrzeit_minute,
'termin_ende_uhrzeit_stunde' => $termin_ende_uhrzeit_stunde,
'termin_ende_uhrzeit_minute' => $termin_ende_uhrzeit_minute,
'termin_schulfrei' => $termin_schulfrei,
'termin_intern' => $termin_intern,
'termin_jahr' => $termin_jahr,
'termin_monat' => $termin_monat,
);
您可以在foreach
数组上进行$group_posts
循环,然后按如下所示访问每个项目:
foreach($group_posts as $group_post) {
// Outputs $titel variable which you added to array above
echo $group_post['title'];
}