WordPress的,从URL中获取多个类别的ID
问题描述:
我使用WordPress 2.8.4。WordPress的,从URL中获取多个类别的ID
我的问题是,如果我查看子类别(cat-slug-2
在这个例子中)有一个内置的函数来获取它的类别ID及其父母类ID?
下面是一个例子URL,其中cat-slug-2
是cat-slug-1
http://www.foo.com/category/cat-slug-1/cat-slug-2/
答
也许是这样的一个子类?
<?php
$current_category = single_cat_title("", false);
$category_ID = $wpdb->get_var("SELECT term_id FROM $wpdb->terms WHERE slug = '" . $current_category . "'");
echo(get_category_parents($category_ID, TRUE, ' » '));
?>
关于上述使用的WP功能/模板标签的详细信息,请参阅single_cat_title和get_category_parents。
答
感谢您的回答Manzabar,从您的代码我能够修改它得到我想要的。
最终,我想要一个类别的parentID数组。以下是我做的:
$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>';
注意$猫持有当前的categoryID