首页 > 解决方案 > Woocommerce 3 Storefront 主题主页上的自定义排序类别

问题描述

在此处输入图像描述 我在主页上显示类别列表。我需要最后一个类别排在第二位。我研究了排序选项,但它们不适合。按名称,按 ID,随机。ID 为“50”的类别需要在列表中排在第二位。

add_filter( 'storefront_product_categories_args', 'custom_storefront_product_categories');
function custom_storefront_product_categories( $args ) {
    $args['limit'] = 9;
    $args['columns'] = 3;
    $args['orderby'] = 'id'; // sort by id category
    return $args;
}

编辑:

我尝试使用以下方法添加产品类别排序 ID:

function custom_storefront_category_filtering( $args ) {
    $args['ids'] = '16,50,17,18,19,20,21,22,23'; // need such a sequence of categories
    return $args;
}
add_filter('storefront_product_categories_shortcode_args','custom_storefront_category_filtering' );

但它只包括产品类别。

标签: phpwordpresssortingwoocommercecustom-taxonomy

解决方案


由于产品类别是自定义分类法idids甚至ID不起作用。相反,您将使用which 是该上下文中参数term_id的适当参数值。orderby

为什么?因为我们的目标不是帖子 ID ,而是术语 ID

所以在你的过滤器钩子中:

add_filter( 'storefront_product_categories_args', 'custom_storefront_product_categories');
function custom_storefront_product_categories( $args ) {
    $args['limit'] = 9;
    $args['columns'] = 3;
    $args['orderby'] = 'term_id';

    return $args;
}

代码位于您的活动子主题(或活动主题)的 function.php 文件中。测试和工作。


推荐阅读