我有以下片段:
if ($listings->have_posts()) {
while ($listings->have_posts()) : $listings->the_post();
if (have_rows('ticket_group')) :
$ticket_group_count = count(get_field('ticket_group'));
echo "<br>Number of ticket_groups: $ticket_group_count<br>";
while (have_rows('ticket_group')) : the_row();
echo 'Checking ticket_group...<br>';
endwhile;
endif;
endwhile;
}
在一页上它向我显示:
Number of ticket_groups: 3
Checking ticket_group...
Checking ticket_group...
Checking ticket_group...
这是我所期望的,因为它说有三个票组。然而,另一方面,它向我显示以下内容,即只有一条记录,但总共有三条记录
Number of ticket_groups: 3
Checking ticket_group...
我正在使用高级自定义字段插件,但我不知道这会如何导致此问题。
关于如何进一步调试有什么建议吗?
循环之前相当大,所以我已将其精简到基本内容,但它仍然在发生。
我添加了
print_r
的 get_field('ticket_group')
并得到以下内容,表明确实有三项:
Array
(
[0] => Array
(
[ticket_group_name] => Coach Travel (Weekend Return)
[ticket_group_start] =>
[ticket_group_finish] =>
[ticket_group_details] => Array
(
[0] => Array
(
[package_name] => Return Coach Travel - Thursday arrival, Monday return
[package_object] => WP_Post Object
(
[ID] => 2164
[post_author] => 7991
[post_date] => 2023-12-13 19:59:58
[post_date_gmt] => 2023-12-13 19:59:58
[post_content] => ...
[post_title] => BLOODSTOCK 2024 - THURSDAY ARRIVAL - MONDAY ARRIVAL - RETURN COACH TRAVEL
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => bloodstock-2024-thursday-arrival-monday-arrival-return-coach-travel
[to_ping] =>
[pinged] =>
[post_modified] => 2024-04-30 17:03:25
[post_modified_gmt] => 2024-04-30 16:03:25
[post_content_filtered] =>
[post_parent] => 0
[guid] => https://biggreencoach.co.uk/package/bloodstock-2024-thursday-arrival-monday-arrival-return-coach-travel/
[menu_order] => 0
[post_type] => package
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[package_start_date] => 01/01/2024 12:00 am
[package_end_date] => 13/08/2024 12:00 pm
[package_on_sale_date] => 2024-02-06 11:00
[package_type] => Array
(
[0] => Weekend
[1] => Return
[2] => Coach
)
)
)
)
[1] => Array
(
[ticket_group_name] => Weekend Festival Ticket + Coach Travel Package
[ticket_group_start] =>
[ticket_group_finish] =>
[ticket_group_details] => Array
(
[0] => Array
(
[package_name] => Weekend Camping Ticket + Return Coach - Thursday arrival, Monday return
[package_object] => WP_Post Object
(
[ID] => 2165
[post_author] => 7991
[post_date] => 2023-12-13 19:59:58
[post_date_gmt] => 2023-12-13 19:59:58
[post_content] => ...
[post_title] => BLOODSTOCK 2024 - THURSDAY ARRIVAL - MONDAY DEPARTURE - RETURN COACH TRAVEL + FESTIVAL TICKET
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => bloodstock-2024-thursday-arrival-monday-departure-return-coach-travel-festival-ticket
[to_ping] =>
[pinged] =>
[post_modified] => 2023-12-13 19:59:58
[post_modified_gmt] => 2023-12-13 19:59:58
[post_content_filtered] =>
[post_parent] => 0
[guid] => https://biggreencoach.co.uk/package/bloodstock-2024-thursday-arrival-monday-departure-return-coach-travel-festival-ticket/
[menu_order] => 0
[post_type] => package
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[package_start_date] => 01/01/2024 12:00 am
[package_end_date] => 13/08/2024 12:00 pm
[package_on_sale_date] => 2024-01-03 00:00
[package_type] => Array
(
[0] => Weekend
[1] => Return
[2] => Coach
[3] => Festival Ticket
[4] => Camping
)
)
)
)
[2] => Array
(
[ticket_group_name] => Shuttle Bus (Weekend Return)
[ticket_group_start] =>
[ticket_group_finish] =>
[ticket_group_details] => Array
(
[0] => Array
(
[package_name] => Shuttle Bus - Thursday arrival, Monday return
[package_object] => WP_Post Object
(
[ID] => 3864
[post_author] => 7991
[post_date] => 2024-01-26 10:43:53
[post_date_gmt] => 2024-01-26 10:43:53
[post_content] => ...
[post_title] => BLOODSTOCK 2024 - SHUTTLE SERVICE
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => bloodstock-2024-shuttle-service
[to_ping] =>
[pinged] =>
[post_modified] => 2024-04-30 17:04:08
[post_modified_gmt] => 2024-04-30 16:04:08
[post_content_filtered] =>
[post_parent] => 0
[guid] => https://biggreencoach.co.uk/package/bloodstock-2024-shuttle-service/
[menu_order] => 0
[post_type] => package
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[package_start_date] => 01/01/2024 12:00 am
[package_end_date] => 13/08/2024 12:00 pm
[package_on_sale_date] => 2024-01-03 00:00
[package_type] => Array
(
[0] => Weekend
[1] => Return
[2] => Shuttle
)
)
)
)
)
您似乎遇到了一个问题,即您的循环没有迭代其中一页上
ticket_group
的所有行,即使数组包含三个项目。以下是一些有助于调试问题的建议:
检查 ACF 设置:确保在高级自定义字段 (ACF) 中为您正在使用的帖子类型正确设置
ticket_group
字段。确认它是转发器字段并且包含相关帖子的数据。
重置发布数据:第一次循环后,请确保在进入
wp_reset_postdata()
循环之前使用have_rows
重置发布数据。这可以帮助避免与任何全局变量发生冲突。
if ($listings->have_posts()) {
while ($listings->have_posts()) : $listings->the_post();
if (have_rows('ticket_group')) :
$ticket_group_count = count(get_field('ticket_group'));
echo "<br>Number of ticket_groups: $ticket_group_count<br>";
while (have_rows('ticket_group')) : the_row();
echo 'Checking ticket_group...<br>';
endwhile;
endif;
endwhile;
wp_reset_postdata(); // Reset the post data here
}
调试输出:在进入
have_rows
循环之前添加更多调试语句以检查发生了什么:
echo "<pre>";
print_r(get_field('ticket_group'));
echo "</pre>";
检查条件逻辑:如果有任何条件或逻辑影响
ticket_group
字段的显示(如用户权限、字段可见性设置等),请确保这些条件或逻辑不会限制行。
交叉检查帖子数据:确保您正在查看
ticket_group
数据应该所在的正确帖子。您可能希望在检查字段之前显式打印帖子 ID 或标题。
echo "Current Post ID: " . get_the_ID() . "<br>";
检查错误:检查错误日志是否存在可能无法立即看到的任何潜在问题。
检查字段名称:确保您的代码和 ACF 字段设置之间没有拼写错误或不匹配的字段名称。
ACF 版本:有时 ACF 更新可能会出现问题。确保您的 ACF 插件已更新到最新版本。
通过实施这些调试技术,您应该能够识别可能导致
ticket_group
循环问题的原因。如果您需要进一步的帮助,请告诉我!