Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress,如果循环没有结果,则don';不显示标题_Php_Wordpress_Loops_Conditional - Fatal编程技术网

Php Wordpress,如果循环没有结果,则don';不显示标题

Php Wordpress,如果循环没有结果,则don';不显示标题,php,wordpress,loops,conditional,Php,Wordpress,Loops,Conditional,我使用WP_查询在wordpress中循环浏览一个自定义帖子类型。我的循环如下所示: <div class="bigRedStrip"> <h2>Available Now!</h2> </div> <ul> <?php $loop = new WP_Query( array( 'post_type' => 'films', 'post_child' => 0, 'posts_per_page' =&

我使用WP_查询在wordpress中循环浏览一个自定义帖子类型。我的循环如下所示:

<div class="bigRedStrip">
    <h2>Available Now!</h2>
</div>

<ul>
    <?php $loop = new WP_Query( array( 'post_type' => 'films', 'post_child' => 0, 'posts_per_page' => 8,'orderby' => 'date', 'order' => 'ASC', 'film-categories' => 'blu-ray' ) ); ?>

    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

            <li>
             loop stuff here
            </li>

    <?php endwhile; ?>
</ul>

现在可用!
  • 循环的东西在这里

如您所见,在循环之前有一个标题,上面写着“Available Now!”。我想重新格式化循环,这样如果没有返回帖子,那么包含标题的div(div类bigRedStrip)将不会显示。我已经尝试了许多可能的解决方案,但我一直遇到的问题是,所有这些“解决方案”都需要将
放入循环中,这会导致每个返回的帖子都重复标题。这样做的目的是让标题只显示一次。你知道我怎样才能做到这一点吗?

你只需要把事情分开一点。首先,运行查询:

<?php $loop = new WP_Query( array( 'post_type' => 'films', 'post_child' => 0, 'posts_per_page' => 8,'orderby' => 'date', 'order' => 'ASC', 'film-categories' => 'blu-ray' ) ); ?>

然后检查是否存在以下情况:

<?php if ($loop->have_posts()) { ?>

<div class="bigRedStrip">
    <h2>Available Now!</h2>
</div>

...

现在可用!
...
如果是这样的话,只需反复浏览以下帖子:

...

<ul>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

            <li>
             loop stuff here
            </li>

    <?php endwhile; ?>
</ul>

<?php } ?>
。。。
  • 循环的东西在这里

  • 循环的东西在这里

  • WP\u Query
    尚未实现。谢谢,只是更改了它+我也是。这个想法很好,顺便说一句,我添加了一个功能请求:
    <?php $loop = new WP_Query( array( 'post_type' => 'films', 'post_child' => 0, 'posts_per_page' => 8,'orderby' => 'date', 'order' => 'ASC', 'film-categories' => 'blu-ray' ) ); ?>
    <?php if ($loop->have_posts()){
    <div class="bigRedStrip">
            <h2>Available Now!</h2>
    </div>
    
    <ul>
        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
            <li>
             loop stuff here
            </li>
    
        <?php endwhile; ?>
    </ul>
    <?php } ?>