我创建了一个代码来以列表格式显示事件。一切正常,但是如果没有返回数据,则会显示空白页。因此,我想简单地显示一条消息"未找到事件"。
<?php
// declaring data
global $post;
$all_events = tribe_get_events(array(
'eventDisplay' => 'upcoming',
'tag' => 'swiss',
'posts_per_page' => 100
));
?>
<!-- fetching events based on upcoming and tags. -->
<?php foreach ($all_events as $post): ?>
<?php setup_postdata($post); ?>
<a href="<?php the_permalink(); ?>">
<div class="col-md-12 event_col">
<div class="col-md-4"><span>- <?php echo tribe_get_start_date($post->ID, false, 'dS M, Y | h:i a'); ?></span></div>
<div class="col-md-4"><span><?php echo tribe_get_venue(); ?></span></div>
<div class="col-md-4"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
</div>
</a>
<?php endforeach; ?>
<?php wp_reset_query(); ?>
首先,您需要检查数组是否为空,如果数组为空,则不允许进入条件。如果数组不为空,则允许,您可以在其中显示详细信息,而在 else 部分中,您可以像未找到帖子一样显示消息。
if(!empty($all_events))
{
##here you can enter code for display the detail like loop to display the details`
foreach($all_events as $post)
{
##DISPLAY POST DETAILS
}
}
else
{
## here you can show the message of no events found.
}
检查您的$all_events
变量:
if (!empty($all_events)) {
foreach($all_events as $post) {
}
} else {
echo 'No events';
}
检查$all_events是否有值
if(!empty($all_events)){
### do your code
}else{
echo "No Post Found!";
}
}
请在开始每个循环之前检查数组的大小。
if(count($all_events) > 0){ foreach($all_events as $post) { } } else { echo "No Post Found!"; }
希望它有效!