• Hello!

    As you can see on my current project website, Gaming World, topmost parent categories appear in tab form inside a black bar above the content area.

    What I’d like to know is how to list all of the categories related to these tabs in the sidebar, meaning that if you click on one of the tabs or its subcategories or posts, all of the categories that fall under the tab should show up in the side bar.

    For instance, in a setup that looks like this:

    [Games (top level tab)]
      |_ Level 1
        |_ Level 2
          |_ Post
        |_ Level 2
    
    [Applications (top level tab)]
      |_ Post
      |_ Level 1
      |_ Level 1
        |_ Post

    All of the categories that fall under either Games or Applications should be listed in the sidebar if you click on any of the posts or categories under either category.

    I’ve Googled this like a nut without finding anything, and I won’t be able to launch the site until I get this function down.

    ANY help would be appreciated.

    Thanks. πŸ™‚

Viewing 15 replies - 1 through 15 (of 16 total)
  • Thread Starter danielnorstrom

    (@danielnorstrom)

    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.

    Thread Starter danielnorstrom

    (@danielnorstrom)

    Bump.

    Thread Starter danielnorstrom

    (@danielnorstrom)

    Bump again.

    All I really need is a function that gets the top level category ID of a specified category ID.

    Thread Starter danielnorstrom

    (@danielnorstrom)

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

    Thread Starter danielnorstrom

    (@danielnorstrom)

    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

    Thread Starter danielnorstrom

    (@danielnorstrom)

    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.

    Thread Starter danielnorstrom

    (@danielnorstrom)

    Bump.

    Thread Starter danielnorstrom

    (@danielnorstrom)

    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.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Complex category function – Relative conditions’ is closed to new replies.