WordPress to get the current category or parent category ID program code

Source: Internet
Author: User

Specific

Get current Category:

The code is as follows Copy Code

<?php the_category_id (False)?>


We can customize a function to get the ID of the root category on the basis of getting this category ID:

The code is as follows Copy Code

function get_category_root_id ($CAT)
{
$this _category = get_category ($cat); Get current category
while ($this _category->category_parent)//If the current classification has superior classification, the loop
{
$this _category = get_category ($this _category->category_parent); Set current category to superior category (climb up)
}
return $this _category->term_id; Returns the ID number of the root category
}

Finally, the root classification of the current article can be represented by a function get_category_root_id (the_category_id (false)).


Also share a category related hot articles using popularity contest Plug-ins to sample

A plug-in has 2 function available

Akpc_most_popular_in_cat and Akpc_most_popular, respectively, specify a category ID or take the current category and all rankings.

But Akpc_most_popular_in_cat's problem is that the original SQL statement is in the Show_top_ranked_in_cat SQL is

The code is as follows Copy Code

WHERE tt.term_id = ' ". Intval ($cat _id)." '

In this way, you can only find in one category and need to be modified to support multiple category IDs. Changed to the following

  code is as follows copy code

WHERE tt.term_id in (". $cat _id.")

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.