Call the subcategory code under the current category in wordpress

Source: Internet
Author: User
Tags php foreach

On the Enterprise website, when you click root category, the subcategories under the current root category are displayed, which is a common requirement. Most cms can also implement this function. Can I use the wordpress architecture?
The answer is yes. wordpress can also implement this function.
In fact, the wp_list_categorys () function is mainly used. The child_of parameter of this function is a number that displays subcategories under the specified ID (that is, the number entered. In this way, you only need to find the ID of the root category of the current category.

The_category_ID () is used to display the Category ID of the current page. The default value is output. When passing the value as a parameter, it is best to pass in a false parameter, that is, the_category_ID (false) to obtain the current Category ID.
The next step is to obtain the parent ID of the current category, which is also the top priority of this article.
I found a lot of information and did not find anything that can be implemented directly. However, using a function, I can obtain it indirectly:

The code is as follows: Copy code

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
{
$ This_category = get_category ($ this_category-> category_parent); // set the current category to the upper-level category (climb up)
}
Return $ this_category-> term_id; // return the ID of the root category
}


Example 2

1. Add the following code to function. php.

The code is as follows: Copy code

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
{
$ This_category = get_category ($ this_category-> category_parent); // set the current category to the upper-level category (climb up)
}
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.

The code is as follows: Copy code

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


Now everything is ready. Let's implement it!

The code is as follows: Copy code


Wp_list_categories ("child_of =". get_category_root_id (the_category_ID (false). "& depth = 0 & hide_empty = 0 & title_li = ");


Obtains the number of all articles under a specified WordPress category (including subcategories).

 

The code is as follows: Copy code

$ Parent_array = get_categories ('hide _ empty = 0 & parent = 79 ');

// Use the get_categories () function. The parameter indicates hide_empty to display any articles in the subcategory.
// ID of the parent category

Foreach ($ parent_array as $ k => $ v) // Step 1
{

$ Sub_parent_array = get_categories ('parent = '. $ v-> cat_ID );

Foreach ($ sub_parent_array as $ kk => $ vv) // Step 2
  {

$ Three_parent_array = get_categories ('hide _ empty = 0 & parent = '. $ vv-> cat_ID );

Foreach ($ three_parent_array as $ kkk => $ vvv) // Step 3
     {
$ Three_count + = $ vvv-> category_count; // counts the number of articles in the third-pole subcategory.
     }

$ Sub_count + = $ vv-> category_count; // The number of articles in the second subcategory.

  }
$ Count + = $ v-> category_count; // counts the number of articles in the first subcategory.
}

$ Total = $ count + $ sub_count + $ three_count;

// Add the number of articles in Level 1, Level 2, and Level 3 statistics to a variable.

In this way, we use a php foreach loop to count the number of articles under a category with a small amount of code.

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.