wordpress 获取子类别列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2040545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:33:28  来源:igfitidea点击:

Get List of subcategory

wordpresswordpress-theming

提问by Permana

I have this category on my wordpress:

我的 wordpress 上有这个类别:

Test1
  - Sub1OfTest1
  - Sub2OfTest1
Test2
  - Sub1OfTest2
  - Sub2OfTest2

Now iam at url: http://localhost/wordpress/category/test1I write the following code on file category-test1.php

现在http://localhost/wordpress/category/test1我在 url:我在文件上写了以下代码category-test1.php

<?php
$categories =  get_categories('child_of=2');

print_r($categories);
foreach ($categories as $category) : ?>

        <div class="qitem">
            <a href="<?php get_category_link( $category->term_id ); ?>" title="<?php echo $category->name; ?>">
                <?php echo $category->name; ?>
            </a>
            <h4>
                <span class="caption">
                    <?php echo $category->name; ?>
                </span>
            </h4>
            <p>
                <span class="caption">
                    <?php echo $category->description; ?>
                </span>
            </p>
        </div>
<?php
endforeach;
?>

I'm trying to show sub category of Test1 but the code only return array(). What did I miss?

我试图显示 Test1 的子类别,但代码只返回 array()。我错过了什么?

回答by LobsterMan

Is that category empty? By default WordPress hides emptr categories. try:

那个类别是空的吗?默认情况下,WordPress 隐藏 emptr 类别。尝试:

$categories =  get_categories('child_of=2&hide_empty=0');

Edit:Fixed, thank you @Stoep

编辑:已修复,谢谢@Stoep

回答by Adam Hopkinson

The child_ofargument of get_categoriesspecifies the category by it's ID - assuming you created your categories in order, the code get_categories('child_of=2')is probably requesting the subcategories of Sub1OfTest1.

child_of参数get_categories通过其 ID 指定类别 - 假设您按顺序创建了类别,代码get_categories('child_of=2')可能正在请求 Sub1OfTest1 的子类别。

To get the ID of a category, go to Posts → Categories and click on a category. The cat_ID will be in the url of the page.

要获取类别的 ID,请转到帖子 → 类别并单击一个类别。cat_ID 将在页面的 url 中。

回答by T.Todua

try this too - Show wordpress subcategories only

也试试这个 -只显示 wordpress 子类别

that topic might help too, as there are different solutions for showing subcategories.

该主题也可能有所帮助,因为显示子类别有不同的解决方案。

回答by Sparky

//This is coding of getting sub category and sub-sub category

//这是获取子类别和子子类别的编码

    $args = array
    (
        'number'     => $number,
        'orderby'    => 'title',
        'order'      => 'ASC',
        'hide_empty' => false,
        'include'    => array(11,281,28,51,99,93,33,55,107,20),
        'exclude'    => array(32,29),
    );
    $product_categories = get_terms( 'product_cat', $args );
    // echo '<pre>';
    // print_r($product_categories);
    // echo '</pre>';

    foreach($product_categories as $data):
        if($data->slug = 'cooking')
        {
            $child_arg = array('hide_empty'=>false,'parent'=>$data->term_id,'exclude'=>array(32,29));
        }
        else
        {
            $child_arg = array('hide_empty'=>false,'parent'=>$data->term_id);
        }
        $child_terms = get_terms('product_cat',$child_arg);
        // echo "<pre>";
        // print_r($child_terms);
        // echo "</pre>";
        foreach($child_terms as $data1):
            if($data1->slug = 'cooking')
            {
                $sub_child_arg = array('hide_empty'=>false,'parent'=>$data1->term_id,'exclude'=>array(32,29));
            }
            else
            {
                $sub_child_arg = array('hide_empty'=>false,'parent'=>$data1->term_id);
            }
            $sub_child_terms = get_terms('product_cat',$sub_child_arg);
            // echo "<pre>";
            // print_r($sub_child_terms);
            // echo "</pre>";
        endforeach;
    endforeach; 
?>