2009-10-16 106 views
0

我使用WordPress 2.8.4。WordPress的,从URL中获取多个类别的ID

我的问题是,如果我查看子类别(cat-slug-2在这个例子中)有一个内置的函数来获取它的类别ID及其父母类ID

下面是一个例子URL,其中cat-slug-2cat-slug-1

http://www.foo.com/category/cat-slug-1/cat-slug-2/

回答

0

也许是这样的一个子类?

<?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, ' &raquo; ')); 
?> 

关于上述使用的WP功能/模板标签的详细信息,请参阅single_cat_titleget_category_parents

0

感谢您的回答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