doujie1908
2018-01-25 02:17
浏览 174

从下拉菜单中的子类别中获取父级

I have this code that shows all my categories in a dropdown menu.

You can see it running here: https://store.vtxfactory.org/products/

For the parent categories, it works like a charm. Problem is, it doesn't recognize the parent of the child categories, so instead of going to /product-category/cameras-photos/lens/ it goes to /product-category/lens. It works too, but that's not what I'm trying to achieve.

Is there a way to get the parent categories on the url too?

<?php

function replace_id_for_slug($option){
$categories = get_categories("hide_empty=0");

preg_match('/value="(\d*)"/', $option[0], $matches);

$id = $matches[1];

$slug = "";

foreach($categories as $category){
    if($category->cat_ID == $id){
        $slug = $category->slug;
    }
}

return preg_replace("/value=\"(\d*)\"/", "value=\"$slug\"", $option[0]);
}

$select = wp_dropdown_categories("hierarchical=true&hide_empty=0&echo=0&taxonomy=product_cat&value_field=slug&show_option_none=- Search by category...&show_count=1&selected=1&orderby=name");

$select = preg_replace_callback("#<option[^>]*>[^<]*</option>#", "replace_id_for_slug", $select);

echo $select;

?>

<script type="text/javascript"><!--
var dropdown = document.getElementById("cat");
function onCatChange() {
    if ( dropdown.options[dropdown.selectedIndex].value != -1 ) {
        location.href = "<?php echo get_option('home');?>/product-category/"+dropdown.options[dropdown.selectedIndex].value+"/";
    }
}
dropdown.onchange = onCatChange;
--></script>

Thanks.

图片转代码服务由CSDN问答提供 功能建议

我有这段代码在下拉菜单中显示我的所有类别。

您可以在此处看到它: https://store.vtxfactory.org/products/ < / p>

对于父类别,它就像一个魅力。 问题是,它无法识别子类别的父级,因此不会转到 / product-category / cameras-photos / lens / ,而是转到 / product-category / lens 。 它也有效,但这不是我想要实现的目标。

有没有办法在网址上获取父类别?

 &lt;?php 
 
function replace_id_for_slug($ option){
 $ categories = get_categories(“hide_empty = 0”); 
 
 npreg_match('/ value =“(\ d *)”/'  ,$ option [0],$ matches); 
 
 $ id = $ matches [1]; 
 
 $ slug =“”; 
 
foreach($ categories as $ category){
 if(  $ category-&gt; cat_ID == $ id){
 $ slug = $ category-&gt; slug; 
} 
} 
 
返回preg_replace(“/ value = \”(\ d *)\“/  “,”value = \“$ slug \”“,$ option [0]); 
} 
 
 $ select = wp_dropdown_categories(”hierarchical = true&amp; hide_empty = 0&amp; echo = 0&amp; taxonomy = product_cat&amp; value_field  = slug&amp; show_option_none =  - 按类别搜索...&amp; show_count = 1&amp; selected = 1&amp; orderby = name“); 
 
 $ select = preg_replace_callback(”#&lt; option [^&gt;] *&gt;  [^&lt;] *&lt; / option&gt;#“,”replace_id_for_slug“,$ select); 
 
echo $ select; 
 
?&gt; 
 
&lt; script type =”text / javascript“&gt  ;&lt;! -  
var dropdown = document.getElementById(“ca  t“); 
 on on onCatChange(){
 if(dropdown.options [dropdown.selectedIndex] .value!= -1){
 location.href =”&lt;?php echo get_option('home');?  &gt; / product-category /“+ dropdown.options [dropdown.selectedIndex] .value +”/“; 
} 
} 
 ntdowndown.onchange = onCatChange; 
  - &gt;&lt; / script&gt; 
 <  /code>

感谢。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • drau89457 2018-01-25 05:29
    已采纳

    You need to modify your JS to check if the selected option has parent option and then print the parent option as well. This should work for your site, note that it only check for one level up if you'll add another level you might want to consider to use for loop instead.

    $(document).ready(function() {
        $("#cat").change(function () {
            var selected = $("#cat option:selected");
            var destination = "<?php echo get_option('home');?>/product-category/"; 
            if(selected.attr("class") == "level-1"){
               destination += selected.prevAll("option.level-0:first").val()+"/";
            }
            destination += selected.val()+"/";
            location.href = destination;
        });
    });
    
    点赞 评论

相关推荐 更多相似问题