我需要显示类别产品页面(Woocommerce),并通过创建自定义帖子类型(CPT)插件的自定义分类将它们分组。
这里有个图片,我的意思是:https://docs.google.com/drawings/d/1JDxGOO2CCq6aGSUwzows7wtAn9DmshCF7M8g-PdcdUc/edit?usp=sharing
这里是自定义帖子类型(CPT)插件:https://es.wordpress.org/plugins/custom-post-type-ui/
有没有办法做到这一点?我想到了“类别”页面的HOOK,从中可以获取“自定义分类标准”值来对产品进行分组并最终将其打印出来……不确定。
我应该从这个开始吗?
/***********************************************************/
/********** CATEGORY GROUPED BY CPT TAXONOMY ************/
/***********************************************************/
// define the woocommerce_shop_loop callback
function my_woocommerce_shop_loop( $array, $int ) {
Fetch products and Custom Taxonomies values to group them.
};
// add the action
add_action( 'woocommerce_shop_loop', 'my_woocommerce_shop_loop', 10, 2 );
这不是您要问的确切解决方案,但这是替代方法,可能会有所帮助,所以我将其发布。
无论是否使用插件,一种遍历terms
的taxonomy
并显示每个术语的项目的方式,都是下面的代码
<?php
$terms = get_terms([
'taxonomy' => 'YOUR_CUSTOM_TAXONOMY_HERE',
]);
foreach($terms as $term) {
$args = array(
'post_type' => 'YOUR_CUSTOM_POST_TYPE',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'YOUR_CUSTOM_TAXONOMY_HERE',
'field' => 'slug',
'terms' => $term->slug,
)
)
);
$the_new_query = new WP_Query( $args );
if ( $the_new_query->have_posts() ) :
echo '<h1>'.$term->name.'</h1>';
echo '<ul>';
while ( $the_new_query->have_posts() ) : $the_new_query->the_post();
'<li>'.the_title().'</li>';
endwhile;
echo '</ul>';
endif;
}
?>