Filter WooCommerce自定义循环以获取一个特定类别的所有产品
我试图在woo comm插件中找到代码(短代码),该插件列出了一个类别中的所有产品,以便能够对其进行修改。。。1小时后没有运气,仍然没有发现 所以我开始自己编写代码(重新发明轮子),这里是我想要得到的 从category ID=“151”中获取所有产品,并能够输出名称、永久链接等 这是现在的代码,返回所有内容。。。。太多了!我不知道如何过滤它Filter WooCommerce自定义循环以获取一个特定类别的所有产品,filter,categories,woocommerce,Filter,Categories,Woocommerce,我试图在woo comm插件中找到代码(短代码),该插件列出了一个类别中的所有产品,以便能够对其进行修改。。。1小时后没有运气,仍然没有发现 所以我开始自己编写代码(重新发明轮子),这里是我想要得到的 从category ID=“151”中获取所有产品,并能够输出名称、永久链接等 这是现在的代码,返回所有内容。。。。太多了!我不知道如何过滤它 { $args = array( 'post_type' => 'product', 'posts_per_page' =>
{
$args = array(
'post_type' => 'product',
'posts_per_page' => 99
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
//echo get_title()."<br/>";
var_dump($loop);
endwhile;
}
{
$args=数组(
“post_类型”=>“产品”,
“每页帖子数”=>99
);
$loop=新的WP_查询($args);
如果($loop->have_posts()){
而($loop->have_posts()):$loop->the_post();
//echo get_title().“
”;
变量转储($loop);
结束时;
}
这是我找到的代码,可以根据需要修改
function get_me_list_of($atts, $content = null)
{
$args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => $atts[0], 'orderby' => 'rand' );
$loop = new WP_Query( $args );
echo '<h1 class="upp">Style '.$atts[0].'</h1>';
echo "<ul class='mylisting'>";
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<li><a href="'.get_permalink().'">'.get_the_post_thumbnail($loop->post->ID, 'thumbnail').'</a></li>';
endwhile;
echo "</ul>";
wp_reset_query();
}
?>
函数get\u me\u list\u of($atts,$content=null)
{
$args=array('post\u type'=>'product','post\u per\u page'=>10,'product\u cat'=>$atts[0],'orderby'=>'rand');
$loop=新的WP_查询($args);
回显“样式”。$atts[0]”;
回声“- ”;
而($loop->have_posts()):$loop->the_post();
全球$产品;
回音“”;
结束时;
回声“
在/woocommerce/includes/class wc shortcodes.php
中定义的[product\u category]
短代码是一个很好的起点,特别是在woocommerce不断发展的情况下
它的核心只是一个标准的WP_查询,添加了一些额外的代码来进行分页,从Woocommerce设置中设置排序顺序,并检查产品是否标记为可见
因此,如果您去掉与短代码相关的代码,并想要一个函数,该函数只从具有特定slug的类别中获得可见的产品,它将如下所示:
function getCategoryProducts($category_slug) {
// Default Woocommerce ordering args
$ordering_args = WC()->query->get_catalog_ordering_args();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $ordering_args['orderby'],
'order' => $ordering_args['order'],
'posts_per_page' => '12',
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => array( esc_attr( $category_slug ) ),
'field' => 'slug',
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
)
)
);
if ( isset( $ordering_args['meta_key'] ) ) {
$args['meta_key'] = $ordering_args['meta_key'];
}
$products = new WP_Query($args);
woocommerce_reset_loop();
wp_reset_postdata();
return $products;
}
传入slug,您将使用在Woocommerce设置中配置的相同顺序返回标准wordpress posts集合。我遇到了类似的问题,因为我想为“自定义页面”获取“结构”产品,下面是我使用的代码
<ul class="products">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 12,
'product_cat' => 'fabrics'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
woocommerce_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
使用上述代码意味着您将获得默认的内联样式、类和其他必要的标记。什么是atts
,此代码应该返回什么?所有类别?请告诉我类别随机