本文章分享了兩段代碼,用來獲取當前分類或父分類id程序,在wordpress中獲取wordpress自身就定義了一個函數,直接可以得出當前文章的分類ID的函數the_category_ID.
<?php the_category_ID(false) ?>
我們可以在得到這個分類ID的基礎上,自定義一個函數,求根分類的ID,代碼如下:
- function get_category_root_id($cat)
- {
- $this_category = get_category($cat); // 取得當前分類
- while($this_category->category_parent) // 若當前分類有上級分類時,循環
- {
- $this_category = get_category($this_category->category_parent); // 將當前分類設為上級分類(往上爬)
- }//開源軟件:Vevb.com
- return $this_category->term_id; // 返回根分類的id號
- }
最后,當前文章的根分類就可以用函數get_category_root_id(the_category_ID(false))來表示.
另外再分享一個分類相關的熱門文章 利用Popularity Contest插件來實例,這個插件有2個function可用 akpc_most_popular_in_cat和akpc_most_popular,分別是指定一個分類id或取當前分類和所有的排名.
但是akpc_most_popular_in_cat有一點問題,就是原來的sql語句是在show_top_ranked_in_cat里面的sql是如下代碼:
WHERE tt.term_id = ‘”.intval($cat_ID).”‘
這樣,只能在一個分類里面查找,需要修改成支持多個分類id的,改成如下代碼如下:
WHERE tt.term_id in (”.$cat_ID.”)
|
新聞熱點
疑難解答
圖片精選