サポート » 使い方全般 » 希望のターム一覧になりません。

  • 下記コードでターム一覧は取得できました。
    しかし、本来やりたいのは

    親カテゴリ名
    子カテゴリ一覧

    親カテゴリ名
    子カテゴリ一覧

    親カテゴリ名
    子カテゴリ一覧

    という並びです。
    下記コードをどのようにすればそうなるでしょうか?

    初心者ですいませんがよろしくお願いします。

    	// タクソノミ取得
    	$cat = array(
    		'taxonomy' => 'product_cat'
    	);
    
    $terms = get_categories($cat);
    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
        echo '<ul>';
            foreach ( $terms as $term ) {
    	        if($term->parent) 
                echo '<li><a href="http://miki-bx.co.jp/wp/product_cat/' . $taxonomy . '/' . $term->slug . '"><h3>' . $term->name . '</h3>' .get_term_thumbnail( $term->term_taxonomy_id, $size = 'category-thumb', $attr = '' ) . '</a></li>';
        }
        echo '</ul>';
    }

    ヘルプの必要なページ: [リンクを見るにはログイン]

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • モデレーター のむらけい (Kei Nomura)

    (@mypacecreator)

    親→子

    親→子

    っていう順番ですので、foreachは1つじゃなくて入れ子にしてやる必要があります。

    
    <?php /* 親ターム取得 */
    $terms = get_terms('product_cat', array( 'parent' => 0 ) );
    foreach($terms as $term): ?>
    
     //なんかいろいろやる
    
    <?php /* 子ターム取得 */
    $child_terms = get_terms('product_cat', array( 'parent' => $term->term_id ) );
    foreach($child_terms as $child_term): ?>
    
     //なんかいろいろやる
    
    <?php endforeach; //子ターム取得ループここまで ?>
    
    <?php endforeach; //親ターム取得ループここまで  ?>

    みたいな感じで。。。

    モデレーター のむらけい (Kei Nomura)

    (@mypacecreator)

    あと

    <a href=".../product_cat/' . $taxonomy . '/' . $term->slug . '">

    の部分は get_term_link を使ったほうがいいと先輩モデレーターが言ってました^^

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • トピック「希望のターム一覧になりません。」には新たに返信することはできません。