Wordpress 两个 custom_post_type 一个分类法
Wordpress two custom_post_type one taxonomy
我创建了两个自定义 post_type。自定义的名字post是-
1. Brand
2. Ethical
并且我创建了一个分类法。分类法的名称是 - Pharma。两个习俗 post 的共同分类是一个 (pharma).
现在我要,在一页上-
1. Just to display all the names of Pharma Taxonomy.
2. I would like to display only brand custom posts under Pharma Taxonomy.
3. I would like to count only the post of brand custom post under pharma taxonomy.
好的。但是当我用 Pharma Taxonomy 调用 brand custom post_type 然后 ethic custom post 也变成了调用。我想要一个解决方案。
$args = array(
'post_type' => 'brand',
'taxonomy' => 'pharma',
'order' => 'ASC',
'posts_per_page' => -1,
);
$query = new WP_Term_Query($args);
foreach ($query->get_terms() as $term) : ?>
<div class="item_wrap">
<h2><a href="<?php echo esc_url(get_term_link($term->term_id)); ?>"><?php echo $term->name; ?></a></h2>
<span class="count"><?php echo $term->count; ?> <?php _e('brands'); ?></span>
</div>
<?php endforeach;
WP_Term_Query
NOT 在您的 foreach
语句中采用 'post_type'
参数 Here's a list of valid arguments
Docs. However, you could add a WP_Query
Docs。像这样:
$pharm_args = array(
'taxonomy' => 'pharma',
'orderby' => 'name',
'order' => 'ASC',
);
$pharm_query = new WP_Term_Query($pharm_args);
foreach ($pharm_query->get_terms() as $term) :
$count_args = array(
'post_type' => 'brand',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'field' => 'slug',
'terms' => $term->slug,
),
)
);
$count_term_in_cpt = new WP_Query($count_args);
if ($count_term_in_cpt->found_posts) {
?>
<div class='item_wrap'>
<h2><a href="<?php echo esc_url(get_term_link($term->term_id)); ?>"><?php echo $term->name; ?></a></h2>
<span class='count'><?php echo $count_term_in_cpt->found_posts; _e('brands'); ?></span>
</div>
<?php
}
wp_reset_postdata();
endforeach;
这将输出:
我创建了两个自定义 post_type。自定义的名字post是-
1. Brand
2. Ethical
并且我创建了一个分类法。分类法的名称是 - Pharma。两个习俗 post 的共同分类是一个 (pharma).
现在我要,在一页上-
1. Just to display all the names of Pharma Taxonomy.
2. I would like to display only brand custom posts under Pharma Taxonomy.
3. I would like to count only the post of brand custom post under pharma taxonomy.
好的。但是当我用 Pharma Taxonomy 调用 brand custom post_type 然后 ethic custom post 也变成了调用。我想要一个解决方案。
$args = array(
'post_type' => 'brand',
'taxonomy' => 'pharma',
'order' => 'ASC',
'posts_per_page' => -1,
);
$query = new WP_Term_Query($args);
foreach ($query->get_terms() as $term) : ?>
<div class="item_wrap">
<h2><a href="<?php echo esc_url(get_term_link($term->term_id)); ?>"><?php echo $term->name; ?></a></h2>
<span class="count"><?php echo $term->count; ?> <?php _e('brands'); ?></span>
</div>
<?php endforeach;
WP_Term_Query
NOT 在您的 foreach
语句中采用 'post_type'
参数 Here's a list of valid arguments
Docs. However, you could add a WP_Query
Docs。像这样:
$pharm_args = array(
'taxonomy' => 'pharma',
'orderby' => 'name',
'order' => 'ASC',
);
$pharm_query = new WP_Term_Query($pharm_args);
foreach ($pharm_query->get_terms() as $term) :
$count_args = array(
'post_type' => 'brand',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'field' => 'slug',
'terms' => $term->slug,
),
)
);
$count_term_in_cpt = new WP_Query($count_args);
if ($count_term_in_cpt->found_posts) {
?>
<div class='item_wrap'>
<h2><a href="<?php echo esc_url(get_term_link($term->term_id)); ?>"><?php echo $term->name; ?></a></h2>
<span class='count'><?php echo $count_term_in_cpt->found_posts; _e('brands'); ?></span>
</div>
<?php
}
wp_reset_postdata();
endforeach;
这将输出: