一般在建企業站的時候會遇到不同分類下需要顯示當前分類下的子分類,這時就需要用到當前分類下子分類的獲取,下面給大家列舉一個獲取當前分類下子分類的方法.
wordpress獲取當前分類下的子分類
1.在functions.php函數文件中添加以下代碼:
- 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; // 返回根分類的id號
- }
2.然后在需要調用分類的地方添加以下代碼,一般都是在sidebar.php文件中.
- <?php
- wp_list_categories(“child_of=”.get_category_root_id(the_category_ID(false)). //開源軟件:Vevb.com
- “&depth=0&hide_empty=0&title_li=”);
- ?>
在這里需要注一下就是,百度經驗上面也有類似的添加方法,但是會顯示代碼錯誤,因為百度經驗上的添加方法里面第一行的function和get之間漏了一個空格,而且需要注的是,這段函數代碼必須添加在,之間,下面是完整的添加代碼:
- <?php
- 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; // 返回根分類的id號
- }
- ?>
新聞熱點
疑難解答
圖片精選