【问题标题】:Wordpress Loop to display Category and posts underWordpress 循环显示类别和帖子
【发布时间】:2017-08-07 06:24:46
【问题描述】:

我是 worpdress 的新手。我刚刚让这个查询与我制作的自定义帖子类型一起使用,但现在在遵循网站的设计规范时遇到了另一个问题。

此页面应显示其各自类别下的产品。我制作的循环只显示了每个产品,但没有显示所有产品的类别。

我打算让它看起来像这样:

猫

  • 项目
  • 项目
  • 项目
  • 项目

猫

  • 项目
  • 项目
  • 项目
  • 项目

但我只得到这个:

  • 项目
  • 项目
  • 项目

我不是在寻找这样的东西:

  • 项目,标题下的猫
  • 项目,标题下的猫
  • ITEM,标题下的猫

      <?php
    $args = array (
      'post_type' => 'products',
      'posts_per_page' => 10,
    );
    
    $loop = new WP_query( $args );
    
    if($loop->have_posts()):
      while($loop->have_posts()): $loop->the_post(); ?>
        <div class="post-loop-single">
          <div class="thumbnail-img"><a href="<?php the_permalink(); ?>"><span><?php the_post_thumbnail('post-thumb'); ?> </span></a></div>
          <div class="post-loop-text">
            <div class="post-loop-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
          </div>
      </div>
      <?php endwhile;
    endif;
    

    ?>

【问题讨论】:

    标签: wordpress loops wordpress-theming


    【解决方案1】:

    首先使用get_terms() 获取所有产品类别,然后在参数中使用tax_query 获取与该类别相关的产品。请检查下面的代码它对你有帮助。

    $args = array(  
            'orderby'   => 'title',
            'order'     => 'ASC',
        );  
    $product_categories = get_terms( 'product_cat', $args );  
    $count = count($product_categories);  
    if ( $count > 0 ){  
        foreach ( $product_categories as $product_category ) {
    
            echo '<h4><a href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</a></h4>';  
                $args =array('posts_per_page' => -1,
                            'post_type' => 'product',
                            'orderby' => 'title',                                
                            'tax_query' => array('relation' => 'AND',  
                                                array(  'taxonomy' => 'product_cat',
                                                    'field' => 'slug',
                                                    'terms' => $product_category->slug  
                                                    )  
                                                ),  
                            );  
                $products = new WP_Query( $args );  
    
                while ( $products->have_posts() ) { $products->the_post();  ?>  
    
                    <div class="post-loop-single">
                        <div class="thumbnail-img"><a href="<?php the_permalink(); ?>"><span><?php the_post_thumbnail('post-thumb'); ?> </span></a></div>
                        <div class="post-loop-text">
                            <div class="post-loop-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
                        </div>
                    </div>
         <?php  }  wp_reset_query();
    
        }  
    }
    

    【讨论】:

    • 感谢您的回复。代码不知何故对我不起作用。它说它在回声线上有一个“可捕获的错误”。进一步挖掘与我类似的其他问题确实使用了 foreach 循环,但我目前还不明白如何在这种特殊情况下使用它。
    • 请提供有关错误和您尝试的代码的更多信息。如果可能,分享屏幕截图
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多