Wordpress, get ID's of multiple categories fro

2019-06-14 01:05发布

I'm using WordPress 2.8.4.

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?

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/

2条回答
等我变得足够好
2楼-- · 2019-06-14 01:49

Thanks for the answer Manzabar, from your code i was able to modify it to get what i wanted.

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>';

Note that $cat holds the current categoryID

查看更多
Melony?
3楼-- · 2019-06-14 01:52

Maybe something like this?

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

For more info on the WP Functions/Template Tags used above, see single_cat_title and get_category_parents.

查看更多
登录 后发表回答