I want to show a category only if a (custom) post is in that category AND region = $name for that post.
So, e.g. I have a custom post (type “business”) named “Mamma Mia” in child-category: “pizzerias” (and in parent-category “food”), and in region “Rotterdam” (custom taxonomy: “region”, custom taxonomy term: “rotterdam”).
=> display category “pizzerias” (and parent-category “food”)
Only, I have no clue how to accomplish this. I’d appreciate any help on this puzzle.
<?php
// $filter = array('region'=>$name);
$categories = get_categories();
foreach ($categories as $cat)
{
if($cat->parent < 1)
{
$cat_name = $cat->cat_name;
$catid = get_cat_ID( $cat_name );
echo $cat_name. '<br/>';
$args=array(
'orderby' => 'name',
'order' => 'ASC',
'child_of' => $catid
);
$categories=get_categories($args);
foreach($categories as $category) {
echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a><br/>';
}
}
}
// print_r($categories);
?>
This is done in the Query Multiple Taxonomies plugin:
http://plugins.trac.wordpress.org/browser/query-multiple-taxonomies/trunk/core.php?rev=308185#L10
General idea – query for specific set of posts, gather all of their categories, filter, let function beat them in shape.
Generic code:
Issues are – as I remember decent queries for multiple taxonomies will only come in WP 3.1 and this might get very intensive so will take caching (probably for each
region
).On the version of wordpress I’m using which is version 3.1.2. If you were to add ‘taxonomy’ => ‘taxonomy_term’ to the array of args it should work. So here’s a modification to your original code to include the taxonomy in the array. Don’t know the taxonomy name you’re trying to use or not though:
I’ve adapted @rarst’s answer from above to use 3 custom taxonomies. I only want to spit out the terms that are attached to one or more of the posts in the loop.
Here’s my function that I’ve added to functions.php:
Hope this helps someone else.
-J
Forgive me if I’ve missed something, but would it not be as simple as;
In the admin, are you attaching the post to both ‘pizzerias’ and ‘food’ (by ticking both checkboxes), or just ‘pizzerias’?
If the latter,
the_category()
won’t show ‘Food’ by default, so you’ll have to grab the category hierarchy yourself.you can return the taxonomie for custom post with a small funtion:
Also you can use the function of wp-core:
and you can ask for tax with
is_tax()
, see codex for this conditional tagMaybe this helps, when i understand your problem right. Sorry for my bad english.
Thank you all for helping me out – that’s really great. I’d like to share with you what I’ve come up with. I know it is not the smartest solution. The drawback is that a parent category is shown for a region, when there is no custom post attached to it for this region (but there is a custom post attached to it for an other region).
I am using this on a region (taxonomy archive) page.
thanks for your help.
but i think their is no need of foreach or any loop we can get child of any category by using this simple code first
$li_id
get the current cat id and we dynamicaly passed this id tochild_of=$cat_id
and it show the selected cat childs i am using it in my site side bar.