Php 列出父类别的所有子类别
我想在WordPress中列出特定父类别的所有子类别,我是PHP新手,所以我需要一些帮助任何帮助都将不胜感激,我想要下面的代码Php 列出父类别的所有子类别,php,html,wordpress,Php,Html,Wordpress,我想在WordPress中列出特定父类别的所有子类别,我是PHP新手,所以我需要一些帮助任何帮助都将不胜感激,我想要下面的代码 <h2>Maine Category 1</h2> <div class="row"> <div class="col">Subcategory 1</div> <div class="col">Subcategory 2</div> </div> <div cla
<h2>Maine Category 1</h2>
<div class="row">
<div class="col">Subcategory 1</div>
<div class="col">Subcategory 2</div>
</div>
<div class="row">
<div class="col">Subcategory 3</div>
<div class="col">Subcategory 4</div>
</div>
<div class="row">
<div class="col">Subcategory 5</div>
<div class="col">Subcategory 6</div>
</div>
缅因州1类
子类别1
子类别2
子类别3
子类别4
子类别5
子类别6
试试这个注意:15是您的父类别id。
$args = array('child_of' => 15);
$categories = get_categories( $args );
foreach($categories as $category) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
echo '<p> Description:'. $category->description . '</p>';
echo '<p> Post Count: '. $category->count . '</p>';
}
$args=array('=>15的子元素);
$categories=get_categories($args);
foreach($categories作为$category){
回声“类别:”;
回显“Description:”.$category->Description.“”;
回显“Post Count:”.$category->Count.”;
}
试试这个注意:15是您的父类别id。
$args = array('child_of' => 15);
$categories = get_categories( $args );
foreach($categories as $category) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
echo '<p> Description:'. $category->description . '</p>';
echo '<p> Post Count: '. $category->count . '</p>';
}
$args=array('=>15的子元素);
$categories=get_categories($args);
foreach($categories作为$category){
回声“类别:”;
回显“Description:”.$category->Description.“”;
回显“Post Count:”.$category->Count.”;
}
您可以使用wp\u列表\u类别
样本:
$parentcat=$i//类别id
wp_列表_类别(“深度=2&child_of=“.$parentcat.”和title_li=“”)代码>
或者你可以在另一个过程中尝试
<?php
$cat_id = get_query_var('cat');
if( ! empty( $cat_id ) ) {
$category = get_category( $cat_id, ARRAY_A );
if( ! empty( $category ) ) {
// parent category
if( $category['parent'] === '0' ) {
// get child IDs
$terms = get_term_children( $cat_id, 'category' );
foreach( $terms as $term_id ) {
$child_category = get_term( $term_id, 'category' );
// if you need the category
$category_name = $child_category->name;
// do whatever you like with the categories now...
echo '<p>Category: <a href="' . get_category_link( $child_category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $child_category->name ) . '" ' . '>' . $child_category->name.'</a> </p> ';
}
} else {
// in child category
// do the regular loop here, if ( have_posts() ) ...
}
}
}
?>
而且已经贴了
希望它能帮助您:)您可以使用wp\u列表\u类别
样本:
$parentcat=$i//类别id
wp_列表_类别(“深度=2&child_of=“.$parentcat.”和title_li=“”)代码>
或者你可以在另一个过程中尝试
<?php
$cat_id = get_query_var('cat');
if( ! empty( $cat_id ) ) {
$category = get_category( $cat_id, ARRAY_A );
if( ! empty( $category ) ) {
// parent category
if( $category['parent'] === '0' ) {
// get child IDs
$terms = get_term_children( $cat_id, 'category' );
foreach( $terms as $term_id ) {
$child_category = get_term( $term_id, 'category' );
// if you need the category
$category_name = $child_category->name;
// do whatever you like with the categories now...
echo '<p>Category: <a href="' . get_category_link( $child_category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $child_category->name ) . '" ' . '>' . $child_category->name.'</a> </p> ';
}
} else {
// in child category
// do the regular loop here, if ( have_posts() ) ...
}
}
}
?>
而且已经贴了
希望它能帮助您:)嗨,Lemon,谢谢您的快速回复,让我试试这段代码。嗨,Lemon,谢谢您的快速回复,让我试试这段代码。