Conditional categories hack (and a question)
-
Well, first of all hello to all and excuse me for my poor english!
I have done a little hack to be able to show posts from conditional categories… Let me put a very easy to understand example: real estate. Imagine I have a category called Properties with subcategories called Homes, Farms, Cabins and so on. Now, I want all properties to be submitted to one of those categories AND… to one one of the subcatgeories in the category State. This is another category with subcategories like Arkansas, California… So all posts will be under two categories, the property type category (Homes, Farms…) and the state category (Arkansas…).
Now, I can browse any given property category, like Homes, and I can see all posts under this category (well, that’s what categories are for, aren’t they?), no matter what state category they belong to. If I browse a state category, say Arkansas, I get all the posts from this given category no matter what property category they belong to (Homes, Farms…). Fine, that’s what I expect.
But… what if I want to mix/cross both types of categories? What if I only want to browse Homes from Arkansas?
THE HACK
Maybe there is a simpler way to do this, maybe this is something allready implemented in WP and I just missed (passed?) it, so feel free to call this hack stupid, cumbersome, wrong, a waste of time, poorly coded (I’m not a PHP expert) or anything… Anyway, this is what I did:
In the header of the blog I have links to property categories (Homes, Farms…) and in category.php I load a custom sidebar where I have placed a list of states. It is a dynamic list generated this way:
<ul><?php $title = get_catname($cat); $cats = get_categories('child_of=10&hide_empty=0'); $count=1; foreach ((array)$cats as $category) { if ($state && $state == $category->category_nicename) { $catlist = '<li class="current">'.$category->cat_name.'</li>'; } else { $catlist = '<li><a href="'.get_category_link($cat).'?state='.$category->category_nicename.'" title="'.$title.' in '.$category->cat_name.'">'.$title.' in '.$category->cat_name.'</a></li>'; } echo $catlist ; $count++; } ?></ul>
As you can see, it takes all children categories of category #10 (State), and creates links with the URL of the current category plus the parameter (is that the word?) STATE, so if I’m browsing the Homes categories, links in the sidebar look like this:
Homes in Arkansas Homes in California
And so on. And the URL is like this:
site.com/category/properties/homes?state=arkansas site.com/category/properties/homes?state=california
Now, in category.php I look for that parameter STATE, like this:
$state = (isset($_GET['state'])) ? $_GET['state'] : "";
And once I get it, I can start apllying changes, like in:
if ($state) { ... }
Then, I get the ID of the state:
$state_id = get_cat_id($cat_name=$state);
And modify the loop with a custom query:
query_posts(array( 'category__and' => array($cat,$state_id) ));
This way I get only posts that are in both the current category and the given state category. So, if I click on:
site.com/category/properties/homes?state=arkansas
The result is that I only get posts that are in both Homes and Arkansas category. Nice.
Now, the final trick is that in the list generated in the sidebar (the first code I posted) I made it so the current state category is highlighted (with custom HTML and CSS) and not linked (to avoid linking to itself). It’s this part:
if ($state && $state == $category->category_nicename) { $catlist = '<li class="current">'.$category->cat_name.'</li>'; } else { $catlist = '<li><a href="'.get_category_link($cat).'?state='.$category->category_nicename.'" title="'.$title.' in '.$category->cat_name.'">'.$title.' in '.$category->cat_name.'</a></li>'; }
Well, everything works and hopefully this is something others can benefit from. Or, as I said, maybe you know of a better, easier way.
THE QUESTION
I’m not into real estate and I’m not based/focused on the USA market (so I have no use for all these state categories). In fact, and this is the problem, I have a more complex category structure and because of this, and what I want to acomplish, sometimes I feel WP is not the right platform for me. But the only problem I have with WP is with conditional categories so… should I ditch such a great software just because of ONE and only ONE problem? Maybe it is a too big problem, but I really think it must be worth to look for a solution.
What’s the problem then? Back to real state categories (they are really easy to understand) what if under Homes I have two categories like New and Used? This may or may not work:
query_posts(array( 'category__and' => array($cat,$state_id) ));
Why? Because, as in the previous example, it will show post from Homes that also belong to a given state category (Arkansas in the example): posts from Homes that were also posted in Arkansas… What if I don’t have posts in Homes? What if I directly submit posts to either New or Used categories (the ones bellow Homes)? It won’t work. I will get zero results.
Even if the category Homes is empty, if I click the link
site.com/category/homes
I’d get posts from the children categories (New and Used), but if I click
site.com/category/homes?state=arkansas
I will get nothing. Obviously, this is because the Homes category is empty (posts are in the children categories) and in the query
query_posts(array( 'category__and' => array($cat,$state_id) ));
I’m only asking for posts in the Homes category. The big question is… how to solve this?
Well, I can get the children categories array with
$cats = get_term_children($cat, 'category'); $cats[] = $cat;
but… how do I pass this to the query? This is my BIG question. How do I make a conditional query with those children categories? Something like
query_posts(array( 'category__and' => array($cat,$state_id,$sub_cats) ));
won’t work, because there is not a single post belonging to ALL those categories, and something like
query_posts(array( 'category__in' => array($cat,$state_id,$sub_cats) ));
will show ANY posts from ANY of the categories no matter what relation they have (for example, it will show ALL posts from Arkansas, or ALL posts from New or Used, EVEN if they don’t belong to the Homes category).
Well, sorry for a long post and I hope you understood what I said because I’m really lost (my english doesn’t help), I’m very new to PHP and believe me, I tried anything (reading hundreds of posts, burning Google and using the old good try and error technique -that has its limitations).
Thaks for your time and thanks in advance for any help.
- The topic ‘Conditional categories hack (and a question)’ is closed to new replies.