【发布时间】:2011-03-14 14:48:35
【问题描述】:
我有一个非常高级的问题,希望这里有人可以帮助我。我有以下内容:
“产品”的自定义帖子类型
“品牌”的自定义分类
&'category'的标准分类法
每个“产品”都标有“品牌”,并放置在父子类别中
我需要做以下事情:
archive.php 页面显示所有包含“产品”的“类别”,其中包含“品牌”分类,其值为“品牌”谁的页面。 感谢@MikeSchinkel
,这可以很好地使用下面的功能$term = get_query_var('term');
$brand = get_term_by('slug',$term,'brands'); // This here just to illustrate
$categories = get_cross_referenced_terms(array(
'post_type' => 'Products',
'taxonomy' => 'category',
'related_taxonomy' => 'brands',
'term_id' => $brand->term_id
));
其中使用以下函数:
// query to get categories for a specific tag
function get_cross_referenced_terms($args) {
global $wpdb;
$args = wp_parse_args($args,array(
'post_type' => 'Products',
'taxonomy' => 'category',
'related_taxonomy' => 'brands',
'term_id' => 0,
));
extract($args);
$sql = <<<SQL
SELECT DISTINCT
{$wpdb->terms}.*,
COUNT(*) AS post_count
FROM
{$wpdb->terms}
INNER JOIN {$wpdb->term_taxonomy} ON {$wpdb->terms}.term_id={$wpdb->term_taxonomy}.term_id
INNER JOIN {$wpdb->term_relationships} ON {$wpdb->term_taxonomy}.term_taxonomy_id={$wpdb->term_relationships}.term_taxonomy_id
INNER JOIN {$wpdb->posts} ON {$wpdb->term_relationships}.object_id={$wpdb->posts}.ID
INNER JOIN {$wpdb->term_relationships} related_relationship ON {$wpdb->posts}.ID=related_relationship.object_id
INNER JOIN {$wpdb->term_taxonomy} related_term_taxonomy ON related_relationship.term_taxonomy_id=related_term_taxonomy.term_taxonomy_id
INNER JOIN {$wpdb->terms} related_terms ON related_term_taxonomy.term_id=related_terms.term_id
WHERE 1=1
AND related_terms.term_id<>{$wpdb->terms}.term_id
AND {$wpdb->posts}.post_type='%s'
AND {$wpdb->term_taxonomy}.taxonomy='%s'
AND related_term_taxonomy.taxonomy='%s'
AND related_terms.term_id=%d
AND {$wpdb->term_taxonomy}.parent=0
GROUP BY
{$wpdb->terms}.term_id
SQL;
$sql = $wpdb->prepare($sql,$post_type,$taxonomy,$related_taxonomy,$term_id);
$terms = $wpdb->get_results($sql);
return $terms;
}
这会显示指定品牌的所有类别并在现场发挥作用。但现在我需要它能够为类别页面设置“child_of”。所以我需要添加的(我认为)是查询类别 id 或指定类别父级的能力。比如:
'child_of' => $parent_category
我认为这可能是我可以在此处添加到数据库查询中的内容:
AND {$wpdb->term_taxonomy}.parent=0
但它有点超出我的范围!如果有人可以帮助我,我将不胜感激!
谢谢
戴夫
【问题讨论】: