首先,在您的税务查询中(tax_query), 您应该使用\'field\' => \'term_id\' 而不是\'field\' => \'id\' —i、 e.使用term_id 而不是id. 但默认值/字段为term_id (术语ID),尽管如此。
因此,您可以实现这样的分组显示,并使用代码代替从<ul class="event_entries"> 在那之前wp_reset_query(); &mdash;您不需要重置全局/主查询,因为您只是进行一个不涉及全局的辅助查询$wp_query 变量:
<?php
// First, group the posts by the city.
$groups = array();
foreach ( $upcoming_events->posts as $i => $p ) {
    $city = get_field( \'ville\', $p->ID );
    if ( ! isset( $groups[ $city ] ) ) {
        $groups[ $city ] = array();
    }
    $groups[ $city ][] =& $upcoming_events->posts[ $i ];
}
// Optional, sort the city names.
ksort( $groups, SORT_FLAG_CASE );
// Then display the posts.
if ( ! empty( $groups ) ) {
    echo \'<ul class="event_entries">\';
    foreach ( $groups as $city => $posts ) {
        if ( ! empty( $posts ) ) {
            // Display city name.
            echo \'<h3>\' . $city . \'</h3>\';
            // Display posts in that city.
            echo \'<ul>\';
            foreach ( $posts as $post ) {
                $event_start_date = get_field( \'date_de_debut\', $post->ID );
                $event_end_date   = get_field( \'date_de_fin\', $post->ID );
                echo \'<li>Du\' . $event_start_date . \' au \' . $event_end_date . \'</li>\';
            }
            echo \'</ul>\';
        }
    }
    echo \'</ul>\'; // close .event_entries
}
注意,我假设所有帖子都有一个有效的城市。
此外,上述技巧不会改变$upcoming_events 对象,如果您调用setup_postdata() 在代码中,还应该调用wp_reset_postdata() 在代码/函数的末尾。
更新:回应您的评论抱歉,我无法提供完整的教程或详细的指南The Loop, 但查看链接的文章,您至少会了解什么是循环,以及如何使用或使用循环。
但是,我可以从上述代码中告诉您:
当你这样做的时候$upcoming_events = new WP_Query(), 这个$upcoming_events 变量成为WP_Query 类,因此,变量将继承/包含所有(公共)属性(例如。$upcoming_events->posts) 和方法/功能(例如。$upcoming_events->query()) 班级的。您可以通过查看官方参考来了解这些属性和方法here.
我也不明白$upcoming_events->posts as $i => $p\'..什么是$i 和$p??“-$i 是中当前项的索引$upcoming_events->posts 正在循环,而$p 是当前项目的值(即。$upcoming_events->posts[ $i ]) 这是一个WP_Post 实例(&M);但是如果您在理解“当前项”时遇到困难,那么您需要检查PHPmanual 在…上foreach, 或者你可以问或搜索“什么是foreach 在PHP中?“堆栈溢出。。
我也不明白$groups[ $city ][] =& $upcoming_events->posts[ $i ];“-嗯,这只是$groups[ $city ][] = $upcoming_events->posts[ $i ]; (复制;注意= 对=&). 它被称为“通过引用分配”,这基本上有助于提高性能,因为我们不需要将post对象复制到$groups 只需在原文中引用$upcoming_events->posts.
看见this Stack Overflow question 和/或this article 了解更多详细信息。
但基本上,想想电脑中的快捷方式之类的参考;e、 例如,桌面上指向文件(例如文本文件)的图标。所以$my_var =& $file; 就像创建文本文件的快捷方式$my_var = $file; (再次注意= vs公司=&) 就像您将文件复制到了桌面。
我不明白$posts 变量“-如果您的意思是foreach ( $groups as $city => $posts ), 那么$posts 是post对象引用的数组,因此$posts[0] 例如,是对$upcoming_events->posts[0] 这是一个post对象。一、 e.它们都指向完全相同的WP_Post 实例(请参见上文第2点)。
因此,我希望这4点对您有所帮助,但有关PHP特定事物的更多信息,如foreach/循环和引用,请询问堆栈溢出或在Google上搜索等:)