如何在wordpress中按父id获取父类别的子类别
我尝试从父类别中获取所有子类别 就像我的父类是新闻,子类是新闻1新闻2新闻3 使用以下代码获取父类别的子类别如何在wordpress中按父id获取父类别的子类别,wordpress,Wordpress,我尝试从父类别中获取所有子类别 就像我的父类是新闻,子类是新闻1新闻2新闻3 使用以下代码获取父类别的子类别 此代码将包含选定父类别的代码,并在category.php模板文件中进行测试和工作 $this_category = get_category($cat); //echo $this_category->cat_ID; $parent_term_id =$this_category->cat_ID; // term id of parent term //
此代码将包含选定父类别的代码,并在category.php模板文件中进行测试和工作
$this_category = get_category($cat);
//echo $this_category->cat_ID;
$parent_term_id =$this_category->cat_ID; // term id of parent term
//$termchildren = get_terms('category',array('child_of' => $parent_id));
$taxonomies = array(
'taxonomy' => 'category'
);
$args = array(
// 'parent' => $parent_term_id,
'child_of' => $parent_term_id
);
$terms = get_terms($taxonomies, $args);
if (sizeof($terms)>0)
{
echo ' <div class="categories"> ';
echo '<p> Sub Categories of '. get_cat_name( $parent_term_id ) .'</p>';
foreach ( $terms as $term ) {
$term_link = sprintf( '<div class="custom-cats"><a href="%1$s" alt="%2$s">%3$s</a></div>', esc_url( get_category_link( $term->term_id ) ),
esc_attr( sprintf( 'View all posts in %s', 'textdomain' ), $term->name ),
esc_html( $term->name ));
echo sprintf( $term_link );
}
echo '</div><!-- categories div end-->';
}
$this\u category=get\u category($cat);
//echo$this_category->cat_ID;
$parent\u term\u id=$this\u category->cat\u id;//父项的项id
//$termchildren=get_terms('category',array('child_of'=>$parent_id));
$taxonomies=数组(
“分类法”=>“类别”
);
$args=数组(
//“父项”=>$parent\u term\u id,
'child\u of'=>$parent\u term\u id
);
$terms=get_terms($taxonomy,$args);
如果(sizeof($terms)>0)
{
回声';
回显“的子类别”。获取“类别名称($parent\u term\u id)。”;
foreach($terms作为$term){
$term\u link=sprintf(“”,esc\u url(获取类别链接($term->term\u id)),
esc_attr(sprintf('View all posts in%s','textdomain'),$term->name),
esc_html($term->name));
echo sprintf(术语链接);
}
回声';
}
我怎样才能让它为我的作者档案工作?
$this_category = get_category($cat);
//echo $this_category->cat_ID;
$parent_term_id =$this_category->cat_ID; // term id of parent term
//$termchildren = get_terms('category',array('child_of' => $parent_id));
$taxonomies = array(
'taxonomy' => 'category'
);
$args = array(
// 'parent' => $parent_term_id,
'child_of' => $parent_term_id
);
$terms = get_terms($taxonomies, $args);
if (sizeof($terms)>0)
{
echo ' <div class="categories"> ';
echo '<p> Sub Categories of '. get_cat_name( $parent_term_id ) .'</p>';
foreach ( $terms as $term ) {
$term_link = sprintf( '<div class="custom-cats"><a href="%1$s" alt="%2$s">%3$s</a></div>', esc_url( get_category_link( $term->term_id ) ),
esc_attr( sprintf( 'View all posts in %s', 'textdomain' ), $term->name ),
esc_html( $term->name ));
echo sprintf( $term_link );
}
echo '</div><!-- categories div end-->';
}