试图获取当前类别的子类别

时间:2022-10-15 07:37:25

I am trying to get subcategory of current category using wp_dropdown_categories .

我正在尝试使用wp_dropdown_categories获取当前类别的子类别。

onclick of category, i want to get subcategory. I did try using get_categories function with arguments but it's not giving me subcategories. While using has_children is giving me blank array.

onclick of category,我想获得子类别。我确实尝试使用带参数的get_categories函数,但它没有给我子类别。使用has_children给我空白数组。

This is my code:

这是我的代码:

add_action( 'wp_ajax_wp_get_subcategory', 'wp_get_subcategory' );

function wp_get_subcategory() {
    $parent_cat_ID = $_POST['selected_category'];

    $args = array(
     'child_of' => $parent_cat_ID,
     'taxonomy' => 'download_category',
     'hide_empty' => 0,
     'hierarchical' => false,
     'depth'  => 1,
     'parent' => $parent_cat_ID
    );

    if ( isset($parent_cat_ID) ) {
        $has_children = get_categories($args);

        if ( $has_children ) {

            //wp_dropdown_categories($args);
            foreach ($has_children as $category) {

                $option = '<option value="'.$category->cat_ID.'">';
                $option .= $category->cat_name;
                echo $option .= '</option>';

            }
        } else {
            ?><select name="sub_cat_disabled" id="sub_cat_disabled" disabled="disabled"><option>No child categories!</option></select><?php
        }
        die();    
    }
}

2 个解决方案

#1


0  

Try this two example i have take this from some reference website. I hope this may useful to you

试试这两个例子,我从一些参考网站上看到这个。我希望这对你有用

  1. List subcategories if viewing a Category, and brothers / sibling categories if in subcategory.

    如果查看类别,则列出子类别;如果在子类别中,则列出兄弟/兄弟类别。

    <?php
    
     if (is_category()) {
     $this_category = get_category($cat);
    }
    ?>
    
    <?php
     if($this_category->category_parent)
      $this_category = wp_list_categories('orderby=id&show_count=0
      &title_li=&use_desc_for_title=1&child_of='.$this_category->category_parent."&echo=0"); 
      else $this_category = wp_list_categories('orderby=id&depth=1&show_count=0
      &title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID.     "&echo=0");
      if ($this_category) { ?> 
    
      <ul>
      <?php echo $this_category; ?>
       </ul>
        <?php } ?>
    
  2. Suppose the category whose subcategories you want to show is category 10, and that its category “nicename” is “archives”.

    假设您要显示其子类别的类别是类别10,并且其类别“nicename”是“存档”。

    <select name="event-dropdown"> 
    <option value=""><?php echo esc_attr_e( 'Select Event', 'textdomain' ); ?></option> 
     <?php 
     $categories = get_categories( array( 'child_of' => 10 ); 
     foreach ( $categories as $category ) {
     printf( '<option value="%1$s">%2$s (%3$s)</option>',
        esc_attr( '/category/archives/' . $category->category_nicename ),
        esc_html( $category->cat_name ),
        esc_html( $category->category_count )
       );
     }
    ?>
    </select>
    

For Reference: click me

供参考:点击我

#2


2  

That's best if you are getting categories now, as far as your older code looks there is one minor mistake that's why your categories are not showing properly. Just change your

如果您现在正在获取类别,这是最好的,只要您的旧代码看起来有一个小错误,这就是您的类别没有正确显示的原因。只是改变你的

'hierarchical' => false, 

to

'hierarchical' => true,

and so, your categories will be showing up nicely.

所以,你的类别将很好地显示出来。

#1


0  

Try this two example i have take this from some reference website. I hope this may useful to you

试试这两个例子,我从一些参考网站上看到这个。我希望这对你有用

  1. List subcategories if viewing a Category, and brothers / sibling categories if in subcategory.

    如果查看类别,则列出子类别;如果在子类别中,则列出兄弟/兄弟类别。

    <?php
    
     if (is_category()) {
     $this_category = get_category($cat);
    }
    ?>
    
    <?php
     if($this_category->category_parent)
      $this_category = wp_list_categories('orderby=id&show_count=0
      &title_li=&use_desc_for_title=1&child_of='.$this_category->category_parent."&echo=0"); 
      else $this_category = wp_list_categories('orderby=id&depth=1&show_count=0
      &title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID.     "&echo=0");
      if ($this_category) { ?> 
    
      <ul>
      <?php echo $this_category; ?>
       </ul>
        <?php } ?>
    
  2. Suppose the category whose subcategories you want to show is category 10, and that its category “nicename” is “archives”.

    假设您要显示其子类别的类别是类别10,并且其类别“nicename”是“存档”。

    <select name="event-dropdown"> 
    <option value=""><?php echo esc_attr_e( 'Select Event', 'textdomain' ); ?></option> 
     <?php 
     $categories = get_categories( array( 'child_of' => 10 ); 
     foreach ( $categories as $category ) {
     printf( '<option value="%1$s">%2$s (%3$s)</option>',
        esc_attr( '/category/archives/' . $category->category_nicename ),
        esc_html( $category->cat_name ),
        esc_html( $category->category_count )
       );
     }
    ?>
    </select>
    

For Reference: click me

供参考:点击我

#2


2  

That's best if you are getting categories now, as far as your older code looks there is one minor mistake that's why your categories are not showing properly. Just change your

如果您现在正在获取类别,这是最好的,只要您的旧代码看起来有一个小错误,这就是您的类别没有正确显示的原因。只是改变你的

'hierarchical' => false, 

to

'hierarchical' => true,

and so, your categories will be showing up nicely.

所以,你的类别将很好地显示出来。