【问题标题】:Custom sorting categories on Home Page in Woocommerce 3 Storefront themeWoocommerce 3 Storefront 主题中主页上的自定义排序类别
【发布时间】:2019-07-08 18:35:12
【问题描述】:

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

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' );

但它只包括产品类别。

【问题讨论】:

  • 你能改写你的问题吗?您想如何在基于 Storefront 主题的网站的主页上重新排序类别? ids 是什么?
  • @KashifRafique,我想添加自定义排序顺序。标准不允许按正确的顺序排序

标签: php wordpress sorting woocommerce custom-taxonomy


【解决方案1】:

由于产品类别是自定义分类,idids 甚至 ID 都不起作用。相反,您将使用 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 文件中。经过测试并且可以工作。

【讨论】:

  • 嗨!我稍微更新了这个问题,也许它变得更清楚了。不幸的是,这个答案不适合。
  • 答案回答了您最初的问题...如果您想根据 ID 对您的产品类别进行排序,您需要使用 $args['orderby'] = 'term_id';... 您无法像您尝试那样按自定义顺序对它们进行排序。现在你可以使用$args['orderby'] = 'term_order'; 来按“菜单顺序”对它们进行排序,就像在后端一样,你可以按自定义顺序对它们进行排序……
  • 感谢您的回答,但我认为您误解了这个问题。我正在尝试按自己的顺序构建类别。
  • $args['orderby'] = 'term_id'; - 相同的 $args['orderby'] = 'id';
猜你喜欢
  • 2018-04-26
  • 1970-01-01
  • 2016-12-10
  • 2016-08-10
  • 2017-07-03
  • 2012-10-21
  • 1970-01-01
  • 1970-01-01
  • 2016-03-21
相关资源
最近更新 更多