【问题标题】:Wordpress Multiple Loops Not Displaying on Page页面上不显示 Wordpress 多个循环
【发布时间】:2015-09-11 03:27:16
【问题描述】:

我正在尝试在 Wordpress 页面上运行多个循环,以根据类别显示不同的帖子。我正在使用自定义帖子类型和自定义分类法。有人可以让我知道我在下面的代码中缺少什么吗?另外,有没有更好的方法来做到这一点?

<?php function dd_services_template() {
  $first_query = new WP_Query( array('post_type' => 'services', 'posts_per_page' => -1, 'category_name' => 'web') );
  $second_query = new WP_Query( array('post_type' => 'services', 'posts_per_page' => -1, 'category_name' => 'brand') );
  $third_query = new WP_Query( array('post_type' => 'services', 'posts_per_page' => -1, 'category_name' => 'print') );
  $fourth_query = new WP_Query( array('post_type' => 'services', 'posts_per_page' => -1, 'category_name' => 'media') );
?>

<div class="services">
  <ul class="servicesul">
    <?php
      if ( $first_query->have_posts() ) {
      while ( $first_query->have_posts() ) : $first_query->the_post(); ?>
        <li>
          <div class="servicestitle"><?php the_title() ?></div>
          <div class="servicescontent"><?php the_content(); ?></div>
        </li>
      <?php endwhile; } ?>

    <?php
      if ( $second_query->have_posts() ) {
      while ( $second_query->have_posts() ) : $second_query->the_post(); ?>
        <li>
          <div class="servicestitle"><?php the_title() ?></div>
          <div class="servicescontent"><?php the_content(); ?></div>
        </li>
      <?php endwhile; } ?>

    <?php
      if ( $third_query->have_posts() ) {
      while ( $third_query->have_posts() ) : $third_query->the_post(); ?>
        <li>
          <div class="servicestitle"><?php the_title() ?></div>
          <div class="servicescontent"><?php the_content(); ?></div>
        </li>
      <?php endwhile; } ?>

    <?php
      if ( $fourth_query->have_posts() ) {
      while ( $fourth_query->have_posts() ) : $fourth_query->the_post(); ?>
        <li>
          <div class="servicestitle"><?php the_title() ?></div>
          <div class="servicescontent"><?php the_content(); ?></div>
        </li>
      <?php endwhile; } ?>

    <?php wp_reset_postdata(); } ?>

  </ul>
</div>

【问题讨论】:

  • 那么......你到底要返​​回什么?带有空ul的html?任何查询都有效吗?它们都返回空(找到 0 个值)?
  • @Clyff 是的,我唯一得到的是&lt;div class="services"&gt; &lt;ul class="servicesul"&gt; &lt;/ul&gt; &lt;/div&gt;...所有查询都不起作用。

标签: php wordpress while-loop custom-post-type custom-taxonomy


【解决方案1】:

好的,我检查你的代码删除

function dd_services_template(){

和

<?php wp_reset_postdata(); } ?>

而且它运行良好。只需检查您是否正确调用该函数。

顺便说一句:我猜你必须在最后一个 div 标记之后关闭函数。

【讨论】:

  • 如何关闭最后一个div标签后的函数?我想我以前从来没有这样做过……
  • 只需移动 行到最后。你外面有一个
    标签。抱歉,如果我之前不清楚。
  • 啊,明白了...我刚刚这样做了,但前端看起来还是一样。
  • 你能在你的问题中展示如何调用这个 dd_services_template() 函数吗?哦,另一件事,因为我没有这个 posts_type 也没有类别,我的测试只有 posts_per_page 作为参数。你也应该检查你的参数是否有错别字。
  • 好的...我通过依次删除每个 $args 来测试不同的 $args。看起来“category_name”是导致问题的原因。我没有使用正确的参数吗?
  • 猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-09
    • 1970-01-01
    • 2019-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多