'swiss', -6ren">
gpt4 book ai didi

php - 我已经创建了 foreach 循环,但如果它没有返回数据,我想显示 "No Post Found"

转载 作者:行者123 更新时间:2023-12-02 13:45:27 25 4
gpt4 key购买 nike

我创建了一个代码来以列表格式显示事件。一切正常。但如果没有返回数据,它会显示空白页。所以我想简单地显示一条消息“未找到事件”。

<?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(); ?>

最佳答案

首先需要检查数组是否为空,如果数组为空则不允许进入条件。如果数组不为空,则允许,这样您就可以显示详细信息,在其他部分您可以显示消息,例如未找到帖子。

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.
}

关于php - 我已经创建了 foreach 循环,但如果它没有返回数据,我想显示 "No Post Found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58321747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com