Wordpress,从URL获取多个类别的ID [英] Wordpress, get ID's of multiple categories from the URL
问题描述
我正在使用WordPress 2.8.4。
I'm using WordPress 2.8.4.
我的问题是,如果我正在查看子类别( cat-slug- 2
在此示例中)是否有内置函数来获取其类别ID 和其父母类别ID ?
My question is, if I'm viewing a sub category (cat-slug-2
in this example) is there a built in function to get it's category ID and its parents category ID?
这里是一个示例URL,其中 cat-slug-2
是 cat-slug-1 的子类别code>
Here's an example URL, where cat-slug-2
is a sub category of cat-slug-1
http://www.foo.com/category/cat-slug-1/cat-slug-2/
推荐答案
感谢Manzabar的回答,我可以从您的代码中对其进行修改,以得到什么我想要。
Thanks for the answer Manzabar, from your code i was able to modify it to get what i wanted.
最终,我想要一个数组,其中包含该类别的parentID。这是我的操作方式:
Ultimately, I wanted an array of the category's parentIDs. Here's how I did it:
$parents = get_category_parents($cat, false, '^%%^');
$parents = explode('^%%^', $parents);
$parentIDs = array();
foreach($parents as $parent){
if (is_null($parent) || empty($parent)){
continue;
}
$parentIDs[] = get_cat_ID($parent);
}
echo '<pre>';
print_r($parentIDs);
echo '</pre>';
请注意,$ cat拥有当前类别ID
Note that $cat holds the current categoryID
这篇关于Wordpress,从URL获取多个类别的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!