質問

私は私のワードプレスで、このカテゴリがあります:

Test1
  - Sub1OfTest1
  - Sub2OfTest1
Test2
  - Sub1OfTest2
  - Sub2OfTest2

今のURLでIAM: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をのサブカテゴリーを表示しようとしているが、コードが唯一の配列を返します()。私は何を逃したのですか?

役に立ちましたか?

解決

そのカテゴリは空ですか?デフォルトでは、WordPressはemptrカテゴリを非表示にします。試してみてください。

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

編集の固定、

あなた@Stoepに感謝

他のヒント

それはIDだことにより、

child_ofget_categories引数はカテゴリを指定します - 。あなたは、コードget_categories('child_of=2')は、おそらくSub1OfTest1のサブカテゴリを要求しているために、カテゴリを作成したと仮定します。

カテゴリのIDを取得するには、カテゴリー→記事やカテゴリをクリックして行きます。 CAT_IDは、ページのURLになります。

これも試す - のみ

ワードプレスのサブカテゴリを表示

そのトピックには、サブカテゴリを表示するためのさまざまなソリューションがあるので、あまりにも役立つかもしれません。

//これは、サブカテゴリーとサブサブカテゴリーを取得するコード化されました     

    $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; 
?>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top