$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'directory-category',
'pad_counts' => false
);
这让我得到了类别。
我想要的是获取此directory-category
分类法的子类别。
关于如何做到这一点的任何想法?
我不是要求解决方案,只是建议或有人向我展示道路。
谷歌搜索没有帮助:/
答案 0 :(得分:1)
你说你不想直接回答,但基本上你想使用这里找到的get_terms:
https://developer.wordpress.org/reference/functions/get_terms/
SELECT * from prod_term_taxonomy WHERE parent = 0;
更新:
// Using your specific parent taxonomy id of 214 the query is below
global $wpdb;
$results = $wpdb->get_results("SELECT * from prod_term_taxonomy WHERE taxonomy = 'directory-category'");
// then you can use WordPress get_term to query each object to get it's name based on it's term_id. $results will be an array of objects so you will use a foreach loop to loop through to get each $result like this...
$child_cat_array = array();
foreach ($results as $result) {
$term = get_term( $result->term_id, $taxonomy );
$name = $term->name;
// the slug will be used for querying for posts
$slug = $term->slug;
// this will push the slug of the child category into the array for querying posts later
array_push($child_cat_array, $slug);
}
然后您可以像这样修改get_posts查询:
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'directory-category',
'field' => 'slug',
'terms' => $child_cat_array
)
)
);
$postslist = get_posts( $args );
答案 1 :(得分:1)
如果要根据您提供的单个类别显示子类别和相关帖子的列表,则可以使用以下代码。确保使用您自己的分类名称,post_type和术语:
function ow_subcategories_with_posts_by_category( $taxonomy, $post_type, $term ) {
$category = get_term_by( 'slug', $term, $taxonomy );
$cat_id = $category->term_id;
// Get all subcategories related to the provided $category ($term)
$subcategories = get_terms(
array(
'taxonomy' => $taxonomy,
'parent' => $cat_id,
'orderby' => 'term_id',
'hide_empty' => true
)
);
?>
<div>
<?php
// Iterate through all subcategories to display each individual subcategory
foreach ( $subcategories as $subcategory ) {
$subcat_name = $subcategory->name;
$subcat_id = $subcategory->term_id;
$subcat_slug = $subcategory->slug;
// Display the name of each individual subcategory with ID and Slug
echo '<h4>Subcategory: ' . $subcat_name . ' - ID: ' . $subcat_id . ' - Slug: ' . $subcat_slug . '</h4>';
// Get all posts that belong to this specific subcategory
$posts = new WP_Query(
array(
'post_type' => $post_type,
'posts_per_page' => -1, // <-- Show all posts
'hide_empty' => true,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'terms' => $subcat_id,
'field' => 'id'
)
)
)
);
// If there are posts available within this subcategory
if ( $posts->have_posts() ):
?>
<ul>
<?php
while ( $posts->have_posts() ): $posts->the_post();
//Show the title of each post with the Post ID
?>
<li>Post: <?php the_title(); ?> - ID: <?php the_ID(); ?></li>
<?php
endwhile;
?>
</ul>
<?php
else:
echo 'No posts found';
endif;
wp_reset_query();
}
?>
</div>
<?php
}
ow_subcategories_with_posts_by_category( 'name-of-your-taxonomy', 'name-of-your-post-type', 'name-of-your-specific-term' );
“您的分类名称”是主要分类的名称。例如:“ victual_category”
“您的帖子类型名称”是您的帖子类型的名称。例如:“胜利”
“您的特定名称的名称”是您要使用的特定类别的名称,以便可以显示属于该类别的子类别。例如:“食物”
因此,如果我调用该函数:
ow_subcategories_with_posts_by_category( 'victual_category', 'victual', 'food' );
这将显示所有子类别及其所属的属于Victual-Category分类食物分类的帖子:
子类别:开胃菜-ID:35-子弹:开胃菜
子类别:炸玉米饼-ID:36-弹头:炸玉米饼