显示基于 child 个自定义分类的帖子
Show posts based on child custom taxonomies
我有一个名为 cinema 的自定义 CPT,它带有一个名为 "cinema-city" 的自定义分类法,我们可以在其中添加城市和地区作为 sub-child。我想以城市应该放映一次的方式放映或循环放映电影,然后放映 children "districts",然后所有放映的电影都在 child 城市的那些猫下面。
在所附的图片中,您可以看到我想要实现的目标的粗略想法;
see this image.
如有任何帮助,我们将不胜感激。
此致,
静音。
是这样的吗?
get_terms 函数参数可以在这里找到:
https://developer.wordpress.org/reference/classes/wp_term_query/__construct/
get_posts 函数参数可以在这里找到:https://codex.wordpress.org/Template_Tags/get_posts
// Get top level cities
$cities = get_terms(array(
'taxonomy' => 'cinema-city'
));
// Loop cities
foreach($cities as $city){
// Output city html
// Get all child districts by using 'child_of' argument
$districts = get_terms(array(
'taxonomy' => 'cinema-city',
'child_of' => $city->term_id
));
// Loop the child districts
foreach($districts as $district){
// Output distruct output
// Get all posts of thad
$cinemas = get_posts(array(
'post_type' => 'cinema',
'tax_query' => array(
'taxonomy' => 'cinema-city',
'field' => 'id',
'terms' => $district->term_id
)
));
// Loop over the cinemas
foreach($cinemas as $cinema){
// Output cinema output
}
}
}
我有一个名为 cinema 的自定义 CPT,它带有一个名为 "cinema-city" 的自定义分类法,我们可以在其中添加城市和地区作为 sub-child。我想以城市应该放映一次的方式放映或循环放映电影,然后放映 children "districts",然后所有放映的电影都在 child 城市的那些猫下面。 在所附的图片中,您可以看到我想要实现的目标的粗略想法; see this image.
如有任何帮助,我们将不胜感激。
此致,
静音。
是这样的吗?
get_terms 函数参数可以在这里找到: https://developer.wordpress.org/reference/classes/wp_term_query/__construct/
get_posts 函数参数可以在这里找到:https://codex.wordpress.org/Template_Tags/get_posts
// Get top level cities
$cities = get_terms(array(
'taxonomy' => 'cinema-city'
));
// Loop cities
foreach($cities as $city){
// Output city html
// Get all child districts by using 'child_of' argument
$districts = get_terms(array(
'taxonomy' => 'cinema-city',
'child_of' => $city->term_id
));
// Loop the child districts
foreach($districts as $district){
// Output distruct output
// Get all posts of thad
$cinemas = get_posts(array(
'post_type' => 'cinema',
'tax_query' => array(
'taxonomy' => 'cinema-city',
'field' => 'id',
'terms' => $district->term_id
)
));
// Loop over the cinemas
foreach($cinemas as $cinema){
// Output cinema output
}
}
}