5

我的wordpress上有这个类别:

Test1
  - Sub1OfTest1
  - Sub2OfTest1
Test2
  - Sub1OfTest2
  - Sub2OfTest2

现在 iam at url:http://localhost/wordpress/category/test1 我在文件上写了以下代码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;
?>

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

4

4 回答 4

10

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

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

编辑:已修复,谢谢@Stoep

于 2010-01-13T14:30:00.970 回答
0

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

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

于 2010-01-11T11:52:15.457 回答
0

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

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

于 2013-03-27T19:14:41.910 回答
0

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

    $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; 
?>
于 2017-07-08T07:10:16.570 回答