WordPress implements the method of obtaining the name of the parent class category This article comes from the source code library

The example of this article describes how WordPress implements the method to obtain the name of the parent class category. Share it with everyone for your reference. The specific analysis is as follows:

 

There are many ways to get the parent category name in wordpress, such as: get the current ID parent category name, etc., which will not be introduced here, let me introduce several functions to get the parent category name, the method of getting the parent category in WordPress , can be used in single, category, there is a parent category that shows the parent category does not display the current category.

 

The example code is as follows:

 

 

function get_category_root_id($cat)
{
$this_category = get_category($cat); // get 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 as the parent category (climb up)
}
return $this_category->term_id; // Returns the id number of the root category
}

 

How WordPress' Sidebar Shows Subclasses of Top-Level Parent Classes 

 

 

If the category ID is fixed, it can be achieved by using it, but if the category ID is not determined, but you want to automatically bind each category, this function cannot be satisfied. You need to get the root category ID of the current page first. The code is as follows:

 

 

function get_category_root_id($cat)
{
$this_category = get_category($cat); // get 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 as the parent category (climb up)
}
return $this_category->term_id; // Returns the id number of the root category
}

 

Now that everything is ready, let's implement it. The code is as follows:

 

Copy the code The code is as follows: wp_list_categories("child_of=".get_category_root_id(the_category_ID(false)). "&depth=0&hide_empty=0&title_li=");

To get the name of the parent category of the current article, the code is as follows:

 

<?php
$category = get_the_category();
$cat_parent = get_cat_name($category[0]->category_parent);
if (!emptyempty($cat_parent)) {
echo $cat_parent;
} else {
echo $category[0]->cat_name;
}
?>

 

Later, I heard from a friend that a function can implement get_category_parents() 

 

 

The function of the get_category_parents() function is to return a list of parent categories of course categories, including the current category. This function is entirely for navigation.

 

[Function usage] The code is as follows:

 

 

Copy the code The code is as follows: <?php echo get_category_parents($category, $display_link, $separator, $nice_name); ?>

【Parameter Description】 

 

 

$category

 

(integer) Category ID, defaults to the current category ID

 

$display_link

 

(boolean) Whether to create more categorized links.

 

$separator

 

(string) Separator for each category.

 

$nice name

 

(boolean) Whether to return the category alias (default: FALSE, do not return).

 

【Example】

 

Specify different directory articles to apply different templates, output the parent category of the current category, and the separator is '»'

 

The example code is as follows:

 

 

Copy the code The code is as follows: <?php echo get_category_parents($cat, TRUE, ' » '); ?>

 

 

Output: Internet » Blogging » WordPress »

 

I hope this article will help you with your WordPress website.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327073635&siteId=291194637