Wordpress obtains the subcategory of the current category on the category page.

Source: Internet
Author: User
When creating a wordpress topic, you often encounter the problem of displaying subcategories under the current category on the wordpress category page. 1. Now add the following code functionget_category_root_id ($ cat) {$ this_categoryget_category ($ cat) to function. php)

When creating a wordpress topic, you often encounter the problem of displaying subcategories under the current category on the wordpress category page.

1. add the following code to function. php.

Function get_category_root_id ($ cat) {$ this_category = get_category ($ cat); // Obtain the current category while ($ this_category-> category_parent) // if the current category has a parent category, loop {$ this_category = get_category ($ this_category-> category_parent); // set the current category to the upper-level category (climbing)} return $ this_category-> term_id; // return the ID of the root category}

2. paste the following code in the area where the secondary category is to be displayed on the page:

 ';            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 '';        }    }?>

This function displays the subcategories of the current category (secondary category) on the category page and article page ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.