【发布时间】:2020-02-12 18:17:48
【问题描述】:
我遇到了一个奇怪的情况,我不确定我的解决方案是否会奏效,或者是否可行……它适用于 Wordpress 网站,但我希望它是一个通用的循环查询,而不是某些东西特定于 WP... 但让我们尝试一下:
我有一个数组,类似这样(在查找 WP 事件帖子时会发生变化):
$events = Array
(
[0] => WP_Post Object
(
[ID] => 8717
[post_author] => 5
[post_date] => 2020-01-28 14:25:36
[post_parent] => 2766
[post_title] => Film 1
)
[1] => WP_Post Object
(
[ID] => 8716
[post_author] => 5
[post_date] => 2020-01-28 14:25:36
[post_parent] => 2769
[post_title] => Film 2
)
[2] => WP_Post Object
(
[ID] => 8716
[post_author] => 5
[post_date] => 2020-01-28 14:25:39
[post_parent] => 2766
[post_title] => Film 1
)
)
我用 foreach 循环遍历数组:
foreach ( $events as $event ) { global $post;
echo tribe_event_featured_image( $event->ID, 'medium' );
// This outputs an image, pulled from the event post
echo '<h4>'. $event->post_title . '</h4>';
tribe_get_start_date($event, false, $format= 'h:i')
//This outputs a start time, pulled from the event
}
但是,我需要先检查 post_parent 是否相同。如果是这样,我需要将它们一起显示,而不是单独显示这些项目。
所以目前的输出是:
IMAGE
Film 1
10:00
IMAGE
Film 2
12:00
IMAGE
Film 1
14:00
但是,电影 1 和电影 3 具有相同的“post_parent”键值,所以我希望输出为:
IMAGE
Film 1
10:00, 14:00 (Note 14:00 is the time pulled from 3rd object in array)
IMAGE
Film 2
12:00
【问题讨论】:
-
这里有类似的问题和答案。 stackoverflow.com/questions/60192457/…
-
谢谢。我已经看到并尝试实施,但它似乎对我不起作用。您能否使用我的特定代码提供答案?
标签: php arrays wordpress iteration