WordPress gets the subcategory of the current category

In productionWordPressWhen the theme is often met, how to be inWordPressThe category page displays the subcategories under the current category or displays the subcategories of the category on the article page,Especially when doing Chinese enterprise theme, we must use this skill。Today, I’d like to share with you the functions that I used to call subcategory when I was doing enterprise topics。

1.Now?function.phpAdd the following code to it

  • function get_category_root_id($cat)  
  • $this_category = get_category($cat);   
  • while($this_category->category_parent)   
  • {  
  • $this_category = get_category($this_category->category_parent);   
  • }  
  • return $this_category->term_id;   
  • }  
  • 2.Then paste the following code where the secondary classification is to be displayed on the page

  • <?php  
  • if(is_single()||is_category())  
  • {  
  • if(get_category_children(get_category_root_id(the_category_ID(false)))!= “” )  
  • {  
  • echo ‘<ul>’;  
  • echo wp_list_categories(“child_of=”.get_category_root_id(the_category_ID(false)). “&depth=0&hide_empty=0&title_li=&orderby=id&order=ASC”);  
  • echo ‘</ul>’;  
  • }  
  • ?>  
  • The function is to display the sub categories of the current category on the category page and the article page(Secondary classification)。

    Related Article
    The method of forbidding the automatic saving of WordPress articles

    Before, Xiao Bian talked about it《solveWordPressarticleidA simple method of discontinuity》Mentioned inWordPress自动保存article的时间间隔的问题,And today we are talking about direct prohibitionWordPressarticle自动保存的方法,The method is Read more

    How to add user defined upload avatar function in WordPress

    Friends who use WordPress to build a website should know that WordPress itself does not have the function of uploading Read more

    WordPress displays the registration time of the login user in the frontend

    When using WordPress to build a website, some users want to display the user registration time in the frontend, but Read more

    WordPress adds the number of SQL queries at the bottom

    An excellent woodpress theme is definitely that the less SQL consulting is used, the better. How can we see how Read more

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    *
    To prove you're a person (not a spam script), type the security word shown in the picture.
    Anti-spam image