wordpress - Woocommerce 自定义产品类别下拉问题

标签 wordpress drop-down-menu woocommerce categories shortcode

我正在为店面主题开发子主题。我使用产品类别小部件作为标题下的下拉菜单,这完全符合我的需要,尽管我需要相同的(如果可能的话)下拉菜单显示在每个类别页面上,而不仅仅是主页。

我正在定制this code这几乎做到了:

/**
 * WooCommerce Extra Feature
 * --------------------------
 *
 * Register a shortcode that creates a product categories dropdown list
 *
 * Use: [product_categories_dropdown orderby="title" count="0" hierarchical="0"]
 */
add_shortcode( 'product_categories_dropdown', 'woo_product_categories_dropdown' );
function woo_product_categories_dropdown( $atts ) {
    extract( shortcode_atts(array(
        'count'        => '0',
        'hierarchical' => '0',
        'orderby'      => ''
    ), $atts ) );
    ob_start();
    // Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258
    wc_product_dropdown_categories( array(
        'orderby'            => ! empty( $orderby ) ? $orderby : 'order',
        'hierarchical'       => $hierarchical,
        'show_uncategorized' => 0,
        'show_counts'        => $count
    ) );
    ?>
    <script type='text/javascript'>
        /* <![CDATA[ */
        jQuery(function(){
            var product_cat_dropdown = jQuery(".dropdown_product_cat");
            function onProductCatChange() {
                if ( product_cat_dropdown.val() !=='' ) {
                    location.href = "<?php echo esc_url( home_url() ); ?>/?product_cat=" +product_cat_dropdown.val();
                }
            }
            product_cat_dropdown.change( onProductCatChange );
        });
    /* ]]> */
    </script>
    <?php
    return ob_get_clean();
}

现在我需要隐藏计数器并显示空类别。

我还没有得到那个。

如何隐藏计数器并显示空类别?

最佳答案

在你的代码中有:

  • 代码中的一些错误,例如错误的 'show_counts''show_count' (没有 s) …现在隐藏计数器已启用并正常运行。
  • 缺少参数“hide_empty”以显示空类别

在此短代码中,您可以更改以下可选参数:

  • hierarchical 默认禁用(设置为“0”)
  • hide_empty 默认禁用(设置为“0”)
  • show_count 现在默认禁用(设置为“0”)
  • depth 默认禁用(设置为“0”)
  • orderby 默认设置为类别“order”(也可以按名称:“name”)

添加了一个自定义 Hook woocommerce_product_categories_shortcode_dropdown_args 允许扩展自定义......

这是新代码:

add_shortcode( 'product_categories_dropdown', 'woo_product_categories_dropdown' );
function woo_product_categories_dropdown( $atts ) {
    // Attributes
    $atts = shortcode_atts( array(
        'hierarchical' => '0', // or '1'
        'hide_empty'   => '0', // or '1'
        'show_count'   => '0', // or '1'
        'depth'        => '0', // or Any integer number to define depth
        'orderby'      => 'order', // or 'name'
    ), $atts, 'product_categories_dropdown' );

    ob_start();

    wc_product_dropdown_categories( apply_filters( 'woocommerce_product_categories_shortcode_dropdown_args', array(
        'depth'              => $atts['depth'],
        'hierarchical'       => $atts['hierarchical'],
        'hide_empty'         => $atts['hide_empty'],
        'orderby'            => $atts['orderby'],
        'show_uncategorized' => 0,
        'show_count'         => $atts['show_count'],
    ) ) );

    ?>
    <script type='text/javascript'>
        jQuery(function($){
            var product_cat_dropdown = $(".dropdown_product_cat");
            function onProductCatChange() {
                if ( product_cat_dropdown.val() !=='' ) {
                    location.href = "<?php echo esc_url( home_url() ); ?>/?product_cat=" +product_cat_dropdown.val();
                }
            }
            product_cat_dropdown.change( onProductCatChange );
        });
    </script>
    <?php

    return ob_get_clean();
}

代码进入事件子主题(或事件主题)的 function.php 文件。

经过测试并有效。


1) 示例用法 - 所有产品类别和子类别分层显示:

[product_categories_dropdown orderby='name' hierarchical='1']

在 php 代码中你可以这样使用它:

echo do_shortcode("[product_categories_dropdown orderby='name' hierarchical='1']");

或者插入到html标签中:

<?php echo do_shortcode("[product_categories_dropdown orderby='name' hierarchical='1']"); ?>

2) 示例用法 - 仅“主要父级”产品类别:

[product_categories_dropdown depth='1' hierarchical='1']

在 php 代码中你可以这样使用它:

echo do_shortcode("[product_categories_dropdown depth='1' hierarchical='1']");

或者插入到html标签中:

<?php echo do_shortcode("[product_categories_dropdown depth='1' hierarchical='1']"); ?>

关于wordpress - Woocommerce 自定义产品类别下拉问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48547560/

相关文章:

php - 在 Woocommerce 中为特定产品类别添加自定义按钮

PHP 表单 - 需要文本框或下拉列表

android - POPUPMENU 图标未在已发布的 apk 版本中显示

php - 在 WooCommerce 中获取购物车项目变体名称而不是变体 ID

Android webview 加载速度太慢

php - 未捕获的异常 : Call to a member function have_posts() on null View

wordpress - 在 WordPress 中添加类到评论表单

php - WordPress:使用 $wpdb->get_results 而不是 mysqli_query 会导致空变量

wordpress - 按名字以特定字母开头的用户

jquery - jqGrid 列,带有模拟 Html.DropDownListFor 的选择列表