【问题标题】:Display of WooCommerce Product SubCategories with Parent显示带有父级的 WooCommerce 产品子类别
【发布时间】:2017-03-20 04:05:15
【问题描述】:

我正在 WordPress 4.6.1 上设置 Woocommerce 2.6.7。

我在 WooCommerce 设置/产品显示中的设置:

“商店页面显示”=“显示类别”
'默认类别显示' = '显示子类别'。

我的目标是让商店页面和显示子类别的页面的布局都显示为 3 列,如屏幕截图:

要让页面实际显示属于某个类别或子类别的产品,请在一列中显示。我是 Stack Overflow 的新手,所以它不会让我发布超过 2 张图片,但如果你看到下一张图片,你就会明白。

通过我在 Woocommerce 文档中找到的一些 CSS 和代码 sn-p,我设法让属于某个类别的产品按我想要的方式显示。

  add_filter('loop_shop_columns', 'loop_columns');
    if (!function_exists('loop_columns')) {
    function loop_columns() {
    if ( is_product_category() || is_product_tag() ) {
    return 1;
    } 
    // for other archive pages and shop page
    else { 
    return 3;
    }}}

但现在我的子类别也显示在一列中,如下所示:

如何让这个页面像 Shop Page 一样显示为 3 列,并且不影响 Products 的 1 列显示?

基本上,由于我已经通过 Woocommerce 产品显示设置以我想要的方式显示商店,我认为我需要一种方法来仅定位具有父级的子类别,并让它们显示为 3 列。

欢迎任何想法或建议。

【问题讨论】:

    标签: wordpress woocommerce customization


    【解决方案1】:

    经过反复试验,我找到并更改了我在以下位置找到的代码: This site

    最困难的事情是获取进入functions.php 的代码。 其余的只是 CSS。

    这为我的目的完成了工作:

    function ym_are_there_subcategories() {
    global $wp_query;
    
    $current_page = get_queried_object();
    $children = get_term_children( $current_page->term_id, 'product_cat' );
    
    return ( empty( $children ) ? false : true);
    }
    
    
    add_filter( 'loop_shop_columns', 'loop_columns' );
    if ( ! function_exists( 'loop_columns' ) ) {
    function loop_columns() {
    
    if ( is_shop() || ym_are_there_subcategories()  ) {
    
    return 3; 
    } else {
    return 1; 
    } // end if subcats
    } // end loop_columns()
    } // end if function_exists
    

    我希望其他人觉得它有用。这几天让我很困惑。

    显示子类别的页面现在如下所示:

    【讨论】:

      猜你喜欢
      • 2015-12-16
      • 2018-11-09
      • 1970-01-01
      • 2020-09-17
      • 2018-05-17
      • 1970-01-01
      • 2016-06-07
      • 1970-01-01
      • 2022-07-08
      相关资源
      最近更新 更多