显示包括子类别在内的所有类别

时间:2013-10-02 作者:SimonStaton

我试图在该类别下显示所有类别和子类别的列表,我已设法获得所有类别的列表,这些类别是父类别,但我很难检测它是否有子类别,然后在当前循环中显示它们。这是我目前的代码:

$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 />\';

}
我在这里找到了一些关于检测类别是否有子类的函数,但我很难在这个链接下实际显示子类(最多一个级别)。

西蒙,非常感谢你的帮助

5 个回复
最合适的回答,由SO网友:SimonStaton 整理而成

解决方法如下:

$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>\';
    }

  }
 ?>

SO网友:T.Todua
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,\'\');
SO网友:jeyhun_mikayil
<?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>\';

    }
?>
SO网友:drjorgepolanco

如果要显示自定义分类法的所有类别和子类别,请使用此代码并确保提供分类法的slug。

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\' );

SO网友:Mohammad Ayoub Khan

我有非常4行的解决方案,它显示类别和子类别及其帖子数量。

$cats = get_terms(\'category\');
foreach ($cats as $cat) {
  echo $cat->name . "<br>" . "(" . $cat->count . ")"; 
}

结束

相关推荐

如何使用php从外部访问WordPress数据库中的数据

我想与iOS应用程序共享特定wordpress数据库表中的数据。客户端希望通过wordpress表单插件输入数据,这些插件在wp数据库中创建自己的表。我所研究的插件本身没有API,wordpress REST API的示例都使用AJAX,我不熟悉AJAX和/或只能访问wordpress帖子或用户数据中的信息。有没有一种方法可以通过php做到这一点,而不需要直接访问数据库?我担心数据库结构的变化和更新可能会破坏应用程序。