Top level parent taxonomy ID

时间:2013-04-19 作者:Juri

在产品归档页面的顶部,我试图显示产品类别列表。规则应该是列表始终显示顶级的直接子级。

关于WPA有几个答案,我一直在尝试实施(以我的最低技能),但我遗漏了一些东西。

以下是查询:

        <?php  
    $taxonomy     = \'product_cat\';
    $orderby      = \'name\';
    $order        = \'DESC\';
    $show_count   = 0;      // 1 for yes, 0 for no
    $pad_counts   = 0;      // 1 for yes, 0 for no
    $hierarchical = 1;      // 1 for yes, 0 for no
    $title        = \'\';
    $empty        = 0;

   // So now I need to get the top parent id in a variable. I tried these 4 options

     /*
     // option 1
     $queried_object = get_queried_object();
    $term_id = $queried_object->term_id;


    // option 2
    $thisCat = get_category( get_query_var( \'cat\' ) ); 
    while ( intval($thisCat->parent) > 0 ) {
      $thisCat = get_category ( $thisCat->parent );
     }
     $term_id = $thisCat->term_id

    //option 3
    $parent  = get_term_by( \'id\', $term_id, $taxonomy);
    while ($parent->parent != 0){
        $parent  = get_term_by( \'id\', $parent->parent, $taxonomy);
    }


    //option 4
    while ($catid) {
      $cat = get_category($catid); // get the object for the catid
      $catid = $cat->category_parent; // assign parent ID (if exists) to $catid
      // the while loop will continue whilst there is a $catid
      // when there is no longer a parent $catid will be NULL so we can assign our $catParent
      $catParent = $cat->cat_ID;
     }
    */

            // Testing if I have the correct value
    echo \'The returned variable is:\'; print_r($term_id);



    $args2 = array(
      \'taxonomy\'     => $taxonomy,
      \'child_of\'     => $term_id,
      \'orderby\'      => $orderby,
      \'order\'        => $order,
      \'show_count\'   => $show_count,
      \'pad_counts\'   => $pad_counts,
      \'hierarchical\' => $hierarchical,
      \'title_li\'     => $title,
      \'hide_empty\'   => $empty
    );

    $sub_cats = get_categories( $args2 );

    $args = array(
        //\'type\'          => \'post\',
        \'orderby\'       => \'term_group\',
        \'hide_empty\'    => 0,
        \'hierarchical\'  => 0,
        \'parent\'        => 0,
        \'taxonomy\'      => \'product_cat\'
    );
    $test = get_categories( $args ); 
    //print_r($test);

    $cat_id = get_query_var(\'cat\');
    echo $cat_id;

    if (!empty($sub_cats)) { 

    echo \'<ul class="categories">\'; 

    foreach($sub_cats as $sub_category) {

        //echo \'de variabele sub_category\'; print_r($sub_category);

        if ($sub_cats->$sub_category == 0) {
            $thumbnail_id  = get_woocommerce_term_meta( $sub_category->term_id, \'thumbnail_id\', true );
            $image = wp_get_attachment_url( $thumbnail_id );
            $name = $sub_category->name;
            $slug = $sub_category->slug;

            echo \'<li class="cat-item \'.$slug.\'"><a href="\'. get_term_link($sub_category->slug, \'product_cat\') .\'"><img src="\'.$image.\'" " /><span class="txt">\'.$name.\'</span></a></li>\';

            }
        }
        echo \'<li class="cat-item search">\';
        get_product_search_form();
        echo \'</li>\';

    echo \'</ul>\';

     } //endif !empty ?>
谢谢!朱里

0 个回复
结束

相关推荐