繁体   English   中英

Wordpress - 在多种帖子类型上使用的分类法 - 如何只为一个get_terms / get_categories?

[英]Wordpress - taxonomy used on multiple post types - how to get_terms / get_categories for just one?

我已经为多种帖子类型注册了分类法,认为这是设置网站的最佳方式,而不是重复使用相同的分类法。

但是,现在我遇到了一个问题,我需要列出一个帖子类型的已使用的分类法,但是它列出了所有这两种类型的列表分类法。 我该如何解决这个问题? Niether get_categories或get_terms似乎可以选择指定要为其分类的邮政类型。

编辑注意:每种帖子类型也有多个分类

有人可以帮忙吗?

register_taxonomy( 
        'sectors',
        array('case-study', 'resource'),   //used in multiple post types
        [
            'labels' => [
                'name' => __( 'Sectors' ),
                'singular_name' => __( 'Sector' ),
            ],
            'hierarchical' => true,
            'show_admin_column' => true,
        ]
    );

    $sectors = get_categories( array('taxonomy' => 'sectors') );  //prints out selected taxonomies for both case studies and resources when I want just resources.

   $services = get_categories( array('taxonomy' => 'services') );  

试试这个

<?php

     $custom_terms = get_terms('custom_taxonomy_name');

foreach($custom_terms as $custom_term) {
    wp_reset_query();
    $args = array('post_type' => 'custom_post_type_name',
        'tax_query' => array(
            array(
                'taxonomy' => 'custom_taxonomy_name',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            ),
        ),
     );

     $loop = new WP_Query($args);
     if($loop->have_posts()) {
        echo '<h2>'.$custom_term->name.'</h2>';

        while($loop->have_posts()) : $loop->the_post();
            echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';
        endwhile;
     }
}
?>

我发现下面的代码有点像魅力:)

function df_terms_clauses( $clauses, $taxonomy, $args ) {
    if ( isset( $args['post_type'] ) && ! empty( $args['post_type'] ) && $args['fields'] !== 'count' ) {
        global $wpdb;

        $post_types = array();

        if ( is_array( $args['post_type'] ) ) {
            foreach ( $args['post_type'] as $cpt ) {
                $post_types[] = "'" . $cpt . "'";
            }
        } else {
            $post_types[] = "'" . $args['post_type'] . "'";
        }

        if ( ! empty( $post_types ) ) {
            $clauses['fields'] = 'DISTINCT ' . str_replace( 'tt.*', 'tt.term_taxonomy_id, tt.taxonomy, tt.description, tt.parent', $clauses['fields'] ) . ', COUNT(p.post_type) AS count';
            $clauses['join'] .= ' LEFT JOIN ' . $wpdb->term_relationships . ' AS r ON r.term_taxonomy_id = tt.term_taxonomy_id LEFT JOIN ' . $wpdb->posts . ' AS p ON p.ID = r.object_id';
            $clauses['where'] .= ' AND (p.post_type IN (' . implode( ',', $post_types ) . ') OR p.post_type IS NULL)';
            $clauses['orderby'] = 'GROUP BY t.term_id ' . $clauses['orderby'];

            //print_r( $clauses );
        } 
    }
    return $clauses;
}

add_filter( 'terms_clauses', 'df_terms_clauses', 10, 3 );
$terms = get_terms( 'animal_cat', array(
    'orderby'    => 'count',
    'hide_empty' => 0
) );
foreach( $terms as $term ) {

    // Define the query
    $args = array(
        'post_type' => 'animal',
        'animal_cat' => $term->slug
    );
    $query = new WP_Query( $args );

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM