【问题标题】:Woocommerce how to get product categoryWoocommerce如何获取产品类别
【发布时间】:2021-08-05 19:17:53
【问题描述】:

我想在内置代码的页面中为 php 中的类别创建一个下拉列表。我可以成功地为它创建一个简码,但由于简码对 html 不友好,我决定不走这条路。因此,没有任何脚本或 jquery,我正在尝试将 php 代码添加到 div 中

<select class="store-search-input form-control" name="dokan_seller_search" value="<?php

                $taxonomy     = 'product_cat';
                $orderby      = 'name';  
                $show_count   = 0;      // 1 for yes, 0 for no
                $pad_counts   = 0;      // 1 for yes, 0 for no
                $hierarchical = 1;      // 1 for yes, 0 for no  
                $title        = '';  
                $empty        = 0;

                $args = array(
                        'taxonomy'     => $taxonomy,
                        'orderby'      => $orderby,
                        'show_count'   => $show_count,
                        'pad_counts'   => $pad_counts,
                        'hierarchical' => $hierarchical,
                        'title_li'     => $title,
                        'hide_empty'   => $empty
                );
                $all_categories = get_categories( $args );
                foreach ($all_categories as $cat) {
                    if($cat->category_parent == 0) {
                        $category_id = $cat->term_id;       
                        echo '<br /><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>';

                        $args2 = array(
                                'taxonomy'     => $taxonomy,
                                'child_of'     => 0,
                                'parent'       => $category_id,
                                'orderby'      => $orderby,
                                'show_count'   => $show_count,
                                'pad_counts'   => $pad_counts,
                                'hierarchical' => $hierarchical,
                                'title_li'     => $title,
                                'hide_empty'   => $empty
                        );
                        $sub_cats = get_categories( $args2 );
                        if($sub_cats) {
                            foreach($sub_cats as $sub_category) {
                                echo  $sub_category->name ;
                            }   
                        }
                    }       
                }
        ?>" >       
        </select>

这发生了:

任何帮助将不胜感激

感谢 Alex 的帮助更新: 我还没有足够的积分直接添加图片:

<input type="select" class="store-search-input form-control" name="dokan_seller_search" value="<?php
            $args = array(
                'taxonomy'     => 'product_cat',
                'orderby'      => 'name',
                'hierarchical' => true,
                'hide_empty'   => false
            );
            $all_categories = get_categories( $args );

            esc_html('<select class="store-search-input form-control" name="dokan_seller_search">');

            foreach($all_categories as $parent){

                if ($parent->category_parent == 0) {
                        echo '<option value="'.$parent->name.'"><a href="'. get_term_link($parent->slug, 'product_cat') .'">'. $parent->name .'</option>';
                }

                $args2 = array(
                        'taxonomy'     => 'product_cat',
                        'child_of'     => $parent->term_id,
                        'parent'       => $parent->term_id,
                        'hide_empty'   => 'false'
                );
                $sub_cats = get_categories( $args2 );
                if($sub_cats) {
                    foreach($sub_cats as $sub_category) {
                        echo  $sub_category->name;
                    }   
                }

            }
            esc_html('</select>');
            
    ?>">

我期待的结果是一个

【问题讨论】:

  • 嘿,关于如何改进您的问题以增加在这里获得帮助的机会的一些建议:1. 将屏幕截图直接添加到帖子中(不是每个人都可能想要点击链接); 2. 可能会添加对您期望发生的事情与实际发生的事情的描述; 3. 为了帮助任何人通过查看代码来调试您的代码,如果您可以提供由它生成的 RAW HTML(不是渲染的屏幕截图),那将非常有帮助。
  • 您实际上创建了一个没有任何选项的
  • @IvanR 感谢您的建议!会改进的:)
  • 请不要添加有问题的图片。阅读Why not upload images of code/errors when asking a question?
  • @Mayur 好的,谢谢您的留言!

标签: wordpress woocommerce product categories woocommerce-theming


【解决方案1】:

您尝试做的事情对于 .使用由 javascript 触发的下拉菜单会更好,这样您就可以更好地控制下拉菜单中的 DOM。只有当您需要向服务器提交数据时,才应真正使用 select。

更新:这是经过测试和工作的。

<script type="text/javascript">

    //First, some js to trigger the window to relocate when you click an option

    jQuery(document).ready(function(){

        //When the select is triggered
        jQuery('#category-links').on('click', function(){

            //Get the URL from the selected option
            var url = jQuery(this).val();
        
            //Navigate to the url
            window.location.replace(url);

        })

    })
</script>


<?php
//Get the parent terms
$parents = get_terms([
    'taxonomy' => 'product_cat',
    'hide_empty' => false,
    'parent' => 0
]);

//Start the dom for the select
echo '<select id="category-links">';
    echo '<optgroup>';

        //The parent option
        echo '<option value="'.get_term_link($term->term_id).'">'. $term->name .'</option>';

            //Check for children, if there are we'll add them here
            $children = get_term_children($term->term_id, 'product_cat');
            if (!empty($children)) {
                foreach ($children as $child) { 
                    $childterm = get_term_by('id', $child, 'product_cat');
                    echo '<option value="'.get_term_link($childterm->term_id).'">'. $childterm->name .'</option>';
                }
            }

        echo '</optgroup>';

    }

echo '<select>';

【讨论】:

  • 嗨,亚历克斯!感谢您的回复,我同意有更好的方法来做到这一点。您还有其他建议可以做得更好吗?我还测试了您提供的代码,但它不起作用,所以我对其进行了一些调整,但结果并不完整
  • 我将把它放入 wp 安装并立即测试它。
  • @booak 你应该发现上面的合适。如果您查看 jQuerys 下拉切换库,您应该能够调整上面的代码以很好地使用它。但是,上述方法仍然有效。
  • 我已将您的代码添加到
  • 那你加错了。该代码包含一个选择标签,因此您无需将其添加到选择中。用上面的代码替换您已有的选择。当我查看您的 URL 时,我得到的只是 404。
猜你喜欢
  • 2016-01-09
  • 2014-06-27
  • 1970-01-01
  • 2014-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-07
相关资源
最近更新 更多