自定义帖子类型的Get_Categories,并按自定义分类(品牌)进行筛选,并列出已定义类别的子类别

时间:2011-03-08 作者:daveaspinall

感谢@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;
}
这用于类别。php页面,效果很好,但我现在需要在子类别页面上使用相同的代码(理想情况下)?

因此,例如,我将其放在“trainers”的分类页面上,按“nike”品牌对自定义帖子类型“products”进行过滤。

我现在需要它来显示所有儿童类别的“训练者”,也可以通过“nike”品牌来过滤自定义帖子类型的“产品”。

有没有办法简单地添加?

\'category_id\'         => \'$parent_category\',
任何帮助都将不胜感激!谢谢

戴夫

1 个回复
SO网友:kaiser

您可以很简单地获得父类别:它被分配给对象。

$cat = get_category( $q_cat );
// Get the ID
echo $cat->category_parent;

结束

相关推荐

Remove Custom Taxonomy Base

我在WordPress 3.0.4中使用自定义分类法我想知道是否有人知道如何从URL中删除分类库?我见过一些插件,它们可以对类别和标记执行此操作,但不能对自定义分类法执行此操作。例如,我有一个名为“城市”的自定义分类法。我希望我的URL结构是mydomain。com/newyork而不是mydomain。com/城市/纽约是否有我可以使用的功能或东西?