Php 正在检查长度WP_查询(数组())

Php 正在检查长度WP_查询(数组()),php,wordpress,Php,Wordpress,大家好,我正在尝试显示一些信息,如果下面的查询没有结果。类似于没有即将到来的事件 如何检查数组的长度以了解它是否包含帖子 这是密码 <?php $now = date('Ymd'); $frontPageEvents = new WP_Query(array( 'posts_per_page' => 2, 'post_type' => 'eve

大家好,我正在尝试显示一些信息,如果下面的查询没有结果。类似于

没有即将到来的事件

如何检查数组的长度以了解它是否包含帖子

这是密码

<?php
                $now = date('Ymd');
                $frontPageEvents = new WP_Query(array(
                  'posts_per_page' => 2,
                  'post_type' => 'event',
                  'meta_key' => 'event_date',
                  'orderby' => 'meta_value_num',
                  'order' => 'ASC',
                  'meta_query' => array(
                    array(
                      'key' => 'event_date',
                      'compare' => '>=',
                      'value' => $now,
                      'type' => 'numeric'
                    )
                  )


                ));


                  while ($frontPageEvents -> have_posts()){
                    $frontPageEvents -> the_post();

                    ?>


                    <div class="">
                      <div class="">
                        <div class=""> <?php
                        $date = new DateTime(get_field('event_date'));
                         echo $date->format('d');
                        ?> <br> <?php echo $date->format('M'); ?></div>

                      </div>
                      <div class="">
                        <img class=""src="<?php the_post_thumbnail_url('eventFrontThumbnail') ?>" alt="">
                      </div>
                      <div class="">
                        <div class="">
                          <h3> <?php the_title(); ?></h3>
                        </div>
                        <div class=""><?php echo wp_trim_words(get_the_content(),10); ?></div>


                        <div class=""> <p>

                          <a class="btn btn-primary"href="<?php the_permalink(); ?>">read more...</a>
                        </p>
                        </div>
                      </div>

                    </div>
                  <?php } ?>

如果没有upcoimg事件,我想显示一个段落,但要注意使用empty函数检查数组是否为空

if (empty($frontPageEvents)) {
    echo 'There are no upcoming events';
} else {
        while ($frontPageEvents -> have_posts()){
               $frontPageEvents -> the_post();
  ?>

如果有事件,请不要忘记输出事件后的右大括号。

只需使用empty函数检查数组是否为空

if (empty($frontPageEvents)) {
    echo 'There are no upcoming events';
} else {
        while ($frontPageEvents -> have_posts()){
               $frontPageEvents -> the_post();
  ?>

如果有任何事件,请不要忘记输出事件后的右大括号。

只需使用一些条件:

<?php if (!$frontPageEvents->have_posts()){ ?>
    <p> some thing </p>
<?php }

while ($frontPageEvents -> have_posts()){
    $frontPageEvents -> the_post();
?>
...

一些东西

...
函数
have_posts()
返回一个布尔值。您可以在此处阅读文档:


只需使用一些条件:

<?php if (!$frontPageEvents->have_posts()){ ?>
    <p> some thing </p>
<?php }

while ($frontPageEvents -> have_posts()){
    $frontPageEvents -> the_post();
?>
...

一些东西

...
函数
have_posts()
返回一个布尔值。您可以在此处阅读文档: