admin管理员组文章数量:1290316
I am trying to display a list of all categories and sub-categories underneath that category, I have managed to get a list of all categories that are parents but am struggling to detect if it has children and then display them in the current loop. Here is my code at the moment:
$args = array(
'orderby' => 'name',
'parent' => 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a><br />';
}
I have found a few functions on here about detecting if the category has children, but am struggling to actually display the children (up to one level) under this link.
Any help is greatly appreciated, Simon
I am trying to display a list of all categories and sub-categories underneath that category, I have managed to get a list of all categories that are parents but am struggling to detect if it has children and then display them in the current loop. Here is my code at the moment:
$args = array(
'orderby' => 'name',
'parent' => 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a><br />';
}
I have found a few functions on here about detecting if the category has children, but am struggling to actually display the children (up to one level) under this link.
Any help is greatly appreciated, Simon
Share Improve this question asked Oct 2, 2013 at 11:23 SimonStatonSimonStaton 1291 gold badge1 silver badge5 bronze badges5 Answers
Reset to default 2I have very 4 lines solutions, It shows category and sub-categories and their posts count.
$cats = get_terms('category');
foreach ($cats as $cat) {
echo $cat->name . "<br>" . "(" . $cat->count . ")";
}
Solved with the following:
$args = array(
'orderby' => 'name',
'parent' => 0
);
$categories = get_categories( $args );
$first = true;
foreach ($categories as $category) {
if ( $first )
{
echo '<li class="title" style="border-top: 0px;"><a href="acatalog/abovegroundpools.html">'.$category->cat_name.'</a>';
$first = false;
}
else
{
echo '<li class="title"><a href="acatalog/abovegroundpools.html">'.$category->cat_name.'</a>';
}
$theid = $category->term_id;
$children = $wpdb->get_results( "SELECT term_id FROM $wpdb->term_taxonomy WHERE parent=$theid" );
$no_children = count($children);
if ($no_children > 0) {
echo "<ul>";
$args2 = array(
'orderby' => 'name',
'parent' => 2
);
$args2["parent"]=$category->term_id;
$categories2 = get_categories( $args2 );
foreach ($categories2 as $category2) {
echo '<li><a href="acatalog/Inflatable-Hot-Tubs.html">'.$category2->cat_name.'</a></li>';
}
echo '</ul>';
} else {
echo '</li>';
}
}
?>
function my_Categ_tree($catId, $depth){
$depth .= '-'; $output ='';
$args = 'hierarchical=1&taxonomy=category&hide_empty=0&parent='; $categories = get_categories($args . $catId);
if(count($categories)>0){
foreach ($categories as $category) {
$selected = ($cat->term_id=="22") ? " selected": "";
$output .= '<option value="'.$category->cat_ID.'" '.$selected .'>'.$depth.$category->cat_name.'</option>';
$output .= my_Categ_tree($category->cat_ID,$depth);
}
}
return $output;
}
echo my_Categ_tree(0,'');
<?php
// Get top level categories and list them
$args = array('orderby' => 'name', 'parent' => 0);
$categories = get_categories( $args );
foreach ($categories as $category) {
echo $category->cat_name . '<br>';
$args2 = array('orderby' => 'name', 'parent' => $category->cat_ID);
$subcategories = get_categories( $args2 );
echo '<ul>';
// While listing top level categories
// list their child categories
foreach ($subcategories as $subcategory) {
echo '<li>' . $subcategory->cat_name . '</li>';
}
echo '</ul>';
}
?>
If you want to display all your categories and subcategories of custom taxonomy, use this code and make sure to provide the slug of your taxonomy.
function ow_categories_with_subcategories( $taxonomy ) {
// Get the top categories that belong to the provided taxonomy (the ones without parent)
$categories = get_terms(
array(
'taxonomy' => $taxonomy,
'parent' => 0, // <-- No Parent
'orderby' => 'term_id',
'hide_empty' => true // <!-- change to false to also display empty ones
)
);
?>
<div>
<?php
// Iterate through all categories to display each individual category
foreach ( $categories as $category ) {
$cat_name = $category->name;
$cat_id = $category->term_id;
$cat_slug = $category->slug;
// Display the name of each individual category
echo '<h3>Category: ' . $cat_name . ' - ID: ' . $cat_id . ' - Slug: ' . $cat_slug . '</h3>';
// Get all the subcategories that belong to the current category
$subcategories = get_terms(
array(
'taxonomy' => $taxonomy,
'parent' => $cat_id, // <-- The parent is the current category
'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>';
}
?>
</div>
<?php
}
?>
</div>
<?php
}
ow_categories_with_subcategories( 'the_name_of_your_taxonomy' );
本文标签: phpDisplay all categories including sub categories
版权声明:本文标题:php - Display all categories including sub categories 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741496919a2381877.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论