Forums

Complex category function - Relative conditions (17 posts)

  1. danielnorstrom
    Member
    Posted 4 months ago #

    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. :)

  2. esmi
    Member
    Posted 4 months ago #

  3. danielnorstrom
    Member
    Posted 4 months ago #

    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.

  4. danielnorstrom
    Member
    Posted 4 months ago #

    Bump.

  5. danielnorstrom
    Member
    Posted 4 months ago #

    Bump again.

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

  6. danielnorstrom
    Member
    Posted 4 months ago #

    I don't know what the rules for bumping are, but...

    Bump.

  7. iridiax
    Member
    Posted 4 months ago #

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

  8. danielnorstrom
    Member
    Posted 4 months ago #

    Hm, do you think you could show me how you would do this?

  9. mikeboy3
    Member
    Posted 4 months ago #

    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

  10. danielnorstrom
    Member
    Posted 4 months ago #

    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.

  11. danielnorstrom
    Member
    Posted 4 months ago #

    Bump.

  12. doodlebee
    Member
    Posted 4 months ago #

  13. danielnorstrom
    Member
    Posted 4 months ago #

    Well, I don't see how that would help me determine the topmost category of the page I'm currently viewing.

  14. iridiax
    Member
    Posted 4 months ago #

  15. iankgrant
    Member
    Posted 3 months ago #

    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!

  16. iankgrant
    Member
    Posted 3 months ago #

    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.

  17. webdunce
    Member
    Posted 2 months ago #

    i find this question interesting.

    I'm thinking one should use the wordpress function get_category_parents(). it returns a string that is a list of category parents of the current category. the list appears to start with the top-most category and end with the bottom-most (the immediate parent of the current category)...though I could have it backwards.

    You can choose how to have it separated...a comma would probably be good. leave nicenames set to false (don't set it to true as it defaults to false), that way i guess you get category id numbers. then you can split the string on the separator and voila, you've got all the cat IDs from top to bottom (you can get the nice names later via the ID).

    Alternately, you could turn nicenames on make sure you use a separator symbol that will NOT be in any of your category names so that the split only splits on the separators and not in the middle of any cat's name.

Reply

You must log in to post.

About this Topic