通过WooCommerce按类别显示所有产品
-
-
您只需要一个循环.在您的`foreach()`中,运行一个新的`WP_Query()`来获取该术语中的所有产品.然后遍历这些产品.You simply need a loop of loops. Inside your `foreach()`, run a new `WP_Query()` to grab all the products in that term.. and then loop through those.
- 0
- 2014-03-25
- helgatheviking
-
我想我知道如何做到这一点,但是我找不到关于使用PHP按类别列出产品的任何信息(我所能找到的只是短代码废话).如果您可以告诉我该代码是什么样的,那么我应该可以弄清楚其余的代码.I think I understand how to do this, but I can't find anything about listing products by category with PHP (all I can find is shortcode nonsense). If you can show me what that code looks like, I should be able to figure out the rest.
- 0
- 2014-03-25
- JacobTheDev
-
您不需要简码,按类别列出产品只是[税收查询](http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters).You don't need a shortcode, listing products by category is just a [Tax Query](http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters).
- 2
- 2014-03-25
- helgatheviking
-
我知道我不需要简码,我只是说那是我能找到的,这无济于事.您提供的链接看起来很有希望,我明天会试一试,然后报告,谢谢.I knew I didn't need a shortcode, I was saying that's all I could find, which was unhelpful. That link you provided looks promising, I'll give it a shot tomorrow and report back, thanks.
- 0
- 2014-03-25
- JacobTheDev
-
好.如果您仍然遇到问题,请尝试使用新的编码方法来编辑问题,我会看一下.Ok. If you are still stuck, edit your question with your new coding attempt and I'll take a look.
- 1
- 2014-03-25
- helgatheviking
-
1 个回答
- 投票数
-
- 2014-03-26
想通了!下面的代码会自动列出所有类别以及每个类别的帖子!
$args = array( 'number' => $number, 'orderby' => 'title', 'order' => 'ASC', 'hide_empty' => $hide_empty, 'include' => $ids ); $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, 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'product_cat', 'field' => 'slug', // 'terms' => 'white-wines' 'terms' => $product_category->slug ) ), 'post_type' => 'product', 'orderby' => 'title,' ); $products = new WP_Query( $args ); echo "<ul>"; while ( $products->have_posts() ) { $products->the_post(); ?> <li> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </li> <?php } echo "</ul>"; } }
Figured it out! The code below automatically lists all categories and each categories posts!
$args = array( 'number' => $number, 'orderby' => 'title', 'order' => 'ASC', 'hide_empty' => $hide_empty, 'include' => $ids ); $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, 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'product_cat', 'field' => 'slug', // 'terms' => 'white-wines' 'terms' => $product_category->slug ) ), 'post_type' => 'product', 'orderby' => 'title,' ); $products = new WP_Query( $args ); echo "<ul>"; while ( $products->have_posts() ) { $products->the_post(); ?> <li> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </li> <?php } echo "</ul>"; } }
-
真好如果您想变得非常疯狂,则可能需要研究[Transients API](https://codex.wordpress.org/Transients_API)...,这将有助于您避免在每次加载页面时都运行这么多查询.Nice. If you want to get really crazy you might want to look into the [Transients API](https://codex.wordpress.org/Transients_API)... that would help keep you from running so many queries on every page load.
- 0
- 2014-03-26
- helgatheviking
-
如何获得每个类别的图像缩略图?How can I get the image thumbnails for each category?
- 0
- 2016-03-06
- Alyssa Reyes
-
@AlyssaReyes类别本质上没有缩略图.您是否为此为此类别设置了自定义字段?您可以将其发布在新问题中,并提供更多详细信息,并将链接发送给我,以便我更好地理解吗?@AlyssaReyes categories don't inherently have thumbnails; did you set up a custom field for your categories for this? Could you post this in a new question with more detail and send me the link so I can better understand?
- 0
- 2016-03-07
- JacobTheDev
-
谢谢您,您为我节省了一些时间,并设定了正确的方向.我可以改善此答案的唯一方法是使用WooCommerce的内置查询类:WC_Product_Query而不是WP_Query,然后使用foreach循环而不是while循环.由于某些原因,请查看Github查询文档:https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query#description,但是要点是:>"自定义WP_Queries查询可能会随着数据移向自定义表以提高性能,在将来的WooCommerce版本中破坏代码."Thanks man, you saved me some time and set me in the right direction. The only way I could improve this answer is to use WooCommerce's built-in query class: `WC_Product_Query`, instead of `WP_Query`, then use a `foreach` loop instead of a `while` loop. For reasons why, take a look at the Github documentation for the query: https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query#description, but the gist is: > "custom WP_Queries queries is likely to break your code in future versions of WooCommerce as data moves towards custom tables for better performance."
- 1
- 2019-07-05
- UncaughtTypeError
我希望使用WooCommerce将商店中的所有类别显示为标题,并且其所有产品下面都以无序列表的形式列出.这可能吗?我看过几件事,可以显示类别列表或特定类别的产品列表,但没有任何内容可以像我描述的那样循环显示所有内容.
这是我目前用来列出所有类别的内容: