我想知道为什么这个函数中的 foreach 循环在它只经过一次循环后输出数据后就结束了。我在 MySQL 中运行了查询,它返回了我正在寻找的 8 条记录。当我print_r
在 foreach 之前和之后将所有内容打包到数组中时,它会在数组中显示 8 个记录集。
但是,当我在页面上运行该函数时,它只输出第一个记录集然后停止。任何想法为什么会这样?
此外,将这一切塞进一个函数中还有一个很长很愚蠢的理由。只知道,这是我唯一的选择。
function query_hp_hero_events() {
global $wpdb;
$sql = "SELECT SQL_CALC_FOUND_ROWS *, mt2.meta_value AS start_time, mt3.meta_value AS end_time, mt4.meta_value AS start_date, mt5.meta_value AS event_id, mt6.meta_value AS event_meta, mt7.meta_value AS event_thumb FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id) INNER JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id AND mt2.meta_key = 'event_start_time') INNER JOIN wp_postmeta AS mt3 ON (wp_posts.ID = mt3.post_id AND mt3.meta_key = 'event_end_time') INNER JOIN wp_postmeta AS mt4 ON (wp_posts.ID = mt4.post_id AND mt4.meta_key = 'event_start_date') INNER JOIN wp_postmeta AS mt5 ON (wp_posts.ID = mt5.post_id AND mt5.meta_key = 'event_id') INNER JOIN wp_postmeta AS mt6 ON (wp_posts.ID = mt6.post_id AND mt6.meta_key = 'event_meta') INNER JOIN wp_postmeta AS mt7 ON (wp_posts.ID = mt7.post_id AND mt7.meta_key = 'event_thumbnail_url') WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (7) ) AND wp_posts.post_type = 'espresso_event' AND (wp_posts.post_status = 'publish') AND (wp_postmeta.meta_key = 'event_start_date' AND (mt1.meta_key = 'event_start_date' AND CAST(mt1.meta_value AS DATE) >= '" . date('Y-m-d') . "') ) GROUP BY wp_posts.ID ORDER BY CAST(wp_postmeta.meta_value AS DATE) ASC, CAST(mt2.meta_value AS TIME) ASC LIMIT 0, 8";
echo $sql;
$events_query = $wpdb->get_results( $wpdb->prepare( $sql ));
foreach ($events_query as $event_listing) {
$eventDate = strtotime($event_listing->start_date);
$eventEndTime = strtotime($event_listing->end_time);
$eventStartTime = strtotime($event_listing->start_time);
$eventId = $event_listing->event_id;
$eventMeta = $event_listing->event_meta;
$eventThumb = $event_listing->event_thumb;
$newEventMeta = unserialize($eventMeta);
$newEventMeta2 = unserialize($newEventMeta);
$homepageOverride = $newEventMeta2["homepage_override"];
$ticketLink = $newEventMeta2["ticket_link"];
$suiteRentals = $newEventMeta2["suite_rentals_link"];
$vipTickets = $newEventMeta2["vip_ticket_link"];
$groupTicketSales = $newEventMeta2["group_sales_link"];
$parkingLink = $newEventMeta2["parking"];
if ($eventDate >= strtotime(date('Y-m-d')) && $eventEndTime >= strtotime(date('G:i'))) {
$hero = '<div id="event' . $eventId . '">';
$hero .= '<div class="details">';
if ( in_category( 'news' ) ) {
$hero .= '<h1><a href="' . get_permalink() . '">' . $event_listing->post_title . '</a></h1>';
} else {
$hero .= '<h1><a href="/event-registration/?ee=' . $eventId . '">' . $event_listing->post_title . '</a></h1>';
}
if ( isset ( $homepageOverride ) && $homepageOverride != "" ) {
$hero .= '<h2>' . $homepageOverride . '</h2>';
} else {
$hero .= homepage_event_date_range($eventId, $eventStartTime);
}
$hero .= '<ul>';
if ( in_category( 'news' ) ) {
$hero .= '<li><a href="' . get_permalink() . '">More Info</a></li>';
} else {
$hero .= '<li><a href="/event-registration/?ee=' . $eventId . '">More Info</a></li>';
}
if ( isset ( $ticketLink ) && $ticketLink != "" ) { $hero .= '<li><a href="' . $ticketLink . '" rel="external">Buy Tickets</a></li>'; }
if ( isset ( $suiteRentals) && $suiteRentals != "" ) { $hero .= '<li><a href="' . $suiteRentals . '">Suite Rentals</a></li>'; }
if ( isset ( $vipTickets) && $vipTickets != "" ) { $hero .= '<li><a href="' . $vipTickets . '">VIP Seats</a></li>'; }
if ( isset ( $groupTicketSales) && $groupTicketSales != "" ) { $hero .= '<li><a href="' . $groupTicketSales . '">Group Tickets</a></li>'; }
if ( isset ( $parkingLink) && $parkingLink != "" ) { $hero .= '<li><a href="' . $parkingLink . '" rel="external">Parking</a></li>'; }
$hero .= '</ul>';
$hero .= '</div>';
if ( isset( $regexMatch[0] ) && $regexMatch[0] != "" && isset( $youtubeIdMatch[0] ) && $youtubeIdMatch[0] != "") {
$hero .= '<div class="yt-video-player" id="videoplayerdiv1" src="http://www.youtube.com/v/' . $youtubeIdMatch[0] . '?enablejsapi=1&playerapiid=videoplayergen1&version=3"></div>';
} else {
$hero .= '<img src="' . $eventThumb . '" width="595" height="350" alt="" class="heroImg" />';
}
$hero .= '</div>';
}
return $hero;
}
}