That’s not the answer I’m looking for.
The main problem is getting a way to fetch the topmost parent category id of whatever post or category you’re currently viewing.
Bump again.
All I really need is a function that gets the top level category ID of a specified category ID.
I don’t know what the rules for bumping are, but…
Bump.
What about customized display of wp_list_categories and using the conditional tags is_category (for category archives) and in_category or post_is_in_descendant_category (for posts)?
Hm, do you think you could show me how you would do this?
you ca manually define which categories are to be shown with a template tag:
http://codex.wordpress.org/Template_Tags/wp_list_categories#Include_or_Exclude_Categories
Give the whole article a good reading, that should be enough to do what you need
That is not what I am looking for. I am not looking to exclude categories. That is easy. I need to know how to get the topmost category ID of the post or category you are currently viewing.
Well, I don’t see how that would help me determine the topmost category of the page I’m currently viewing.
Just grappling with a very similar problem myself. How about something like this:
function get_parent_category_id($id, $level=0) {
$lookfor = "?cat=";
$endat = "\"";
$separator = "|";
$parent_list = get_category_parents($id, TRUE, $separator, FALSE);
$parents = explode($separator, $parent_list);
$parents[$level] = substr($parents[$level], strpos($parents[$level], $lookfor) + strlen($lookfor));
$parents[$level] = substr($parents[$level], 0, strpos($parents[$level], $endat));
return $parents[$level];
}
It’s a bit of a hack, since it needs to chop up the returned links to find the category ids. I haven’t yet tested it on a site that’s using permalinks, so I’m not sure whether that’ll break it.
Otherwise, if you pass it the category id and the level that you want to retrieve (0 being the top level), it should return the id of the parent category at that level. So, something like this would get the sub-categories of the top-level parent:
wp_list_categories(‘hide_empty=0&parent=’.get_parent_category_id(get_query_var(‘cat’), 0));
Apologies in advance if that’s not what you’re after, but I thought I’d throw it out there just in case!
Further to yesterday’s post, I’ve created a version of that function which works with permalinks (on my site, anyway!). Here goes…
function get_parent_category_id($id, $level=0, $permalinks=false) {
// if we're using permalinks, then we need to get the slug and then look it up
if ($permalinks) {
// might to tweak this, depending on what the links returned by get_category_parents look like
$lookfor = "category/";
$endat = "/\"";
} else {
// just get the id number of the category
$lookfor = "?cat=";
$endat = "\"";
}
$separator = "|";
// get a list of the parents of this category
$parent_list = get_category_parents($id, TRUE, $separator, FALSE);
// turn the list into an array
$parents = explode($separator, $parent_list);
// get the id number of the slug from the link
$parents[$level] = substr($parents[$level], strpos($parents[$level], $lookfor) + strlen($lookfor));
$parents[$level] = substr($parents[$level], 0, strpos($parents[$level], $endat));
if (! $permalinks) {
// no permalinks, so just return the number
return $parents[$level];
} else {
// otherwise, look up the slug and then return the id number
$cat = get_category_by_slug($parents[$level]);
return $cat->term_id;
}
}
There’s an additional parameter indicating whether or not permalinks are used on the site. Like I say, it might not be what you’re after, but it’s solved a similar problem for me.