【问题标题】:Create shortcode for displaying only subcategories by given parent name Woocommerce创建简码以仅显示给定父名称 Woocommerce 的子类别
【发布时间】:2021-04-03 11:04:47
【问题描述】:

如果想要一个显示列表(下拉列表)的简码,仅显示具有给定父 ID 的 woocommerce 中的子类别。
如果可能的话,不用 javascript。
示例短代码 [display_shortcode parent="6459" ]

我发现这段代码显示了一个包含所有类别的下拉列表。 我不太擅长用 PHP 来编辑这段代码 :-(

function woo_product_categories_dropdown( $atts ) {
    extract(shortcode_atts(array(
        'show_count'    => '0',
        'hierarchical'  => '0',
        'orderby'       => ''
    ), $atts));

    ob_start();

    $c = $count;
    $h = $hierarchical;
    $o = ( isset( $orderby ) && $orderby != '' ) ? $orderby : 'order';

    // Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258
    woocommerce_product_dropdown_categories( $c, $h, 0, $o );
    ?>
    <script type='text/javascript'>
    /* <![CDATA[ */
        var product_cat_dropdown = jQuery(".dropdown_product_cat");
        function onProductCatChange() {
            if ( product_cat_dropdown.options[product_cat_dropdown.selectedIndex].value !=='' ) {
                location.href = "<?php echo home_url(); ?>/?product_cat="+product_cat_dropdown.options[product_cat_dropdown.selectedIndex].value;
            }
        }
        product_cat_dropdown.onchange = onProductCatChange;
    /* ]]> */
    </script>
    <?php

    return ob_get_clean();
}```

【问题讨论】:

    标签: woocommerce categories shortcode


    【解决方案1】:

    已修复:

    function woocommerce_subcats_by_id( $atts ) {
        extract( shortcode_atts( array(
            'parent_cat_id' => '',
        ), $atts ) );
     
        $idbyid = get_term_by('id', $parent_cat_id, 'product_cat');
        $product_cat_ID = $idbyid->term_id;
          
        $args = array(
            'taxonomy' => 'product_cat',
            'parent' => $product_cat_ID,
           'hide_empty' => true,
        );
        $subcats = get_categories($args);
        $content = '<ul>';
          foreach ($subcats as $sc) {
            $link = get_term_link( $sc->slug, $sc->taxonomy );
              $content .= '<li><a href="'. $link .'">'.$sc->name.'</a></li>';
          }
        $content .= '</ul>';
        return $content;
    }
    add_shortcode( 'show_sub', 'woocommerce_subcats_by_id' );```
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-01
      • 2016-12-14
      • 1970-01-01
      • 2016-06-25
      • 2012-09-18
      • 1970-01-01
      • 2018-05-17
      • 2016-10-24
      相关资源
      最近更新 更多