显示自定义类别的子类别 - WordPress
Display subcategories of custom categories - WordPress
这是我的:
$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
分类法的子类别。
关于如何做到这一点有什么想法吗?
I'm not asking for a solution, just an advice or someone to show me the road.
Googling didn't help :/
这是截图HERE
你说你不想直接回答,但实际上你想使用 get_terms 在这里找到:
https://developer.wordpress.org/reference/functions/get_terms/
SELECT * 来自 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 );
如果您想根据您提供的单个类别显示子类别列表和相关 post,您可以使用以下代码。确保使用您自己的分类名称、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' );
'name-of-your-taxonomy' 是主要分类法的名称。例如:'victual_category'
'name-of-your-post-type' 是您的 post 类型的名称。例如:'victual'
'name-of-your-specific-term' 是您要使用的特定类别的名称,以便可以显示属于该类别的子类别。例如:'food'
所以如果我调用函数:
ow_subcategories_with_posts_by_category( 'victual_category', 'victual', 'food' );
这将显示所有子类别及其各自的 Posts 属于 Victual-Category 分类法的食物:
子类别:开胃菜 - ID:35 - Slug:开胃菜
- Post:薯条和莎莎酱 - ID:464
- Post: 问题 - ID: 465
子类别:Tacos - ID:36 - Slug:tacos
- Post:Al Pastor - ID:466
- Post:Fish Al Pastor - ID:467
这是我的:
$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
分类法的子类别。
关于如何做到这一点有什么想法吗?
I'm not asking for a solution, just an advice or someone to show me the road.
Googling didn't help :/
这是截图HERE
你说你不想直接回答,但实际上你想使用 get_terms 在这里找到:
https://developer.wordpress.org/reference/functions/get_terms/
SELECT * 来自 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 );
如果您想根据您提供的单个类别显示子类别列表和相关 post,您可以使用以下代码。确保使用您自己的分类名称、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' );
'name-of-your-taxonomy' 是主要分类法的名称。例如:'victual_category'
'name-of-your-post-type' 是您的 post 类型的名称。例如:'victual'
'name-of-your-specific-term' 是您要使用的特定类别的名称,以便可以显示属于该类别的子类别。例如:'food'
所以如果我调用函数:
ow_subcategories_with_posts_by_category( 'victual_category', 'victual', 'food' );
这将显示所有子类别及其各自的 Posts 属于 Victual-Category 分类法的食物:
子类别:开胃菜 - ID:35 - Slug:开胃菜
- Post:薯条和莎莎酱 - ID:464
- Post: 问题 - ID: 465
子类别:Tacos - ID:36 - Slug:tacos
- Post:Al Pastor - ID:466
- Post:Fish Al Pastor - ID:467