使用Get_Terms()列出来自一个定制分类和一个特定内置类别的术语

时间:2011-10-18 作者:alzuwaga

我正在尝试获取分类法(kind)的术语和它们拥有的内容数量,但仅限于特定类别(get\\u parent\\u page\\u slug()=城市名称=类别slug)。这就是我目前的情况:

    $args = array(
    //          \'taxonomy\' => \'tipo\',
                \'hide_empty\' => 1,
            );
    $terms = get_terms(\'kind\', $args);

    //var_dump($terms);exit;

    $count = count($terms);
    echo \'<ul>\';
    echo \'<li><a href="\' . get_bloginfo(\'url\') . \'/\' . get_parent_page_slug() . \'/\' . $pagename . \'/\' . \'" title="\' . sprintf(__(\'View all post filed under %s\', \'my_localization_domain\'), $term->name) . \'">Todos (\' . $count . \')</a></li>\';
    foreach ($terms as $term) {
        echo \'<li><a href="\' . get_bloginfo(\'url\') . \'/\' . get_parent_page_slug() . \'/\' . $pagename . \'/?tipo=\' . $term->slug . \'" title="\' . sprintf(__(\'View all post filed under %s\', \'my_localization_domain\'), $term->name) . \'">\' . $term->name . \' (\'. $term->count .\')</a></li>\';
    }
    echo \'<ul>\';
以防万一,我澄清一下:我是在页面模板中这样做的。我预期的结果如下:

城市名称中的住宿

所有(5)酒店(1)客栈(2)小木屋(1)露营(1)提前感谢

1 个回复
最合适的回答,由SO网友:alzuwaga 整理而成

嗯,我不知道这是否很好,但正在返回预期的结果。。。所以现在这对我来说已经足够了。

function my_custom_get_terms( $my_tax, $my_category, $show_all = true ){
    global $wpdb;
    global $pagename;

    $query = "SELECT Count(wp_posts.ID) AS my_term_count, wp_term_taxonomy.term_taxonomy_id, wp_term_taxonomy.taxonomy, wp_terms.name, wp_terms.term_id, wp_terms.slug AS term_slug, wp_terms.term_group, wp_terms_1.slug
    FROM (((((wp_term_relationships INNER JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id) INNER JOIN wp_terms ON wp_term_taxonomy.term_id = wp_terms.term_id) INNER JOIN wp_posts ON wp_term_relationships.object_id = wp_posts.ID) INNER JOIN wp_term_relationships AS wp_term_relationships_1 ON wp_posts.ID = wp_term_relationships_1.object_id) INNER JOIN wp_term_taxonomy AS wp_term_taxonomy_1 ON wp_term_relationships_1.term_taxonomy_id = wp_term_taxonomy_1.term_taxonomy_id) INNER JOIN wp_terms AS wp_terms_1 ON wp_term_taxonomy_1.term_id = wp_terms_1.term_id
    GROUP BY wp_term_taxonomy.term_taxonomy_id, wp_term_taxonomy.taxonomy, wp_terms.name, wp_terms.term_id, wp_terms.slug, wp_terms.term_group, wp_terms_1.slug
    HAVING (((wp_term_taxonomy.taxonomy)=\'$my_tax\') AND ((wp_terms_1.slug) = \'$my_category\'))
    ORDER BY Count(wp_posts.ID) DESC";

    $datos = $wpdb->get_results($query, OBJECT);
    echo "<ul>";
    $total = 0;
    foreach ( $datos as $dato ) {
        echo \'<li><a href="\' . get_bloginfo(\'url\') . \'/\' . get_parent_page_slug() . \'/\' . $pagename . \'/?tipo=\' . $dato->term_slug . \'">\' . $dato->name . \' (\' . $dato->my_term_count . \')</a></li>\';
        $total += $dato->my_term_count;
    }

    if ( $show_all ){
        echo \'<li><a href="\' . get_bloginfo(\'url\') . \'/\' . get_parent_page_slug() . \'/\' . $pagename . \'/">Todos (\' . $total . \')</a></li>\';
    }
    echo "</ul>";
}



my_custom_get_terms( \'tipo\', get_parent_page_slug(), true );
无论如何谢谢你

结束