製品の主な親カテゴリを取得する
3 回答
- 投票
-
- 2012-07-06
私は「チェーン」をずっと上る独自の関数を作成しました.私の再帰はあなたが見た中で最高の実装ではないかもしれませんが、それは機能します.
function get_parent_terms($term) { if ($term->parent > 0) { $term = get_term_by("id", $term->parent, "product_cat"); if ($term->parent > 0) { get_parent_terms($term); } else return $term; } else return $term; } # Will return all categories of a product, including parent categories function all_cat_classes($post) { $cats = ""; $terms = get_the_terms($post->ID, "product_cat"); $key = 0; // foreach product_cat get main top product_cat foreach ($terms as $cat) { $cat = get_parent_terms($cat); $cats .= (strpos($cats, $cat->slug) === false ? $cat->slug." " : ""); $key++; } return $cats; }
I wrote my own function to go all the way up the "chain". My recursive might not be the best implementation you've seen, but it works.
function get_parent_terms($term) { if ($term->parent > 0) { $term = get_term_by("id", $term->parent, "product_cat"); if ($term->parent > 0) { get_parent_terms($term); } else return $term; } else return $term; } # Will return all categories of a product, including parent categories function all_cat_classes($post) { $cats = ""; $terms = get_the_terms($post->ID, "product_cat"); $key = 0; // foreach product_cat get main top product_cat foreach ($terms as $cat) { $cat = get_parent_terms($cat); $cats .= (strpos($cats, $cat->slug) === false ? $cat->slug." " : ""); $key++; } return $cats; }
-
- 2013-09-11
誰かを助けるかもしれない代替ソリューションを提供するためだけに:
コード
function wc_origin_trail_ancestor( $link = false, $trail = false ) { if (is_product_category()) { global $wp_query; $q_obj = $wp_query->get_queried_object(); $cat_id = $q_obj->term_id; $descendant = get_term_by("id", $cat_id, "product_cat"); $descendant_id = $descendant->term_id; $ancestors = get_ancestors($cat_id, 'product_cat'); $ancestors = array_reverse($ancestors); $origin_ancestor = get_term_by("id", $ancestors[0], "product_cat"); $origin_ancestor_id = $origin_ancestor->term_id; $ac = count($ancestors); } else if ( is_product() ) { $descendant = get_the_terms( $post->ID, 'product_cat' ); $descendant = array_reverse($descendant); $descendant = $descendant[0]; $descendant_id = $descendant->term_id; $ancestors = array_reverse(get_ancestors($descendant_id, 'product_cat')); $ac = count($ancestors); } $c = 1; if( $trail == false ){ $origin_ancestor_term = get_term_by("id", $ancestors[0], "product_cat"); $origin_ancestor_link = get_term_link( $origin_ancestor_term->slug, $origin_ancestor_term->taxonomy ); if($link == true) echo '<a href="'. $origin_ancestor_link .'">'; echo $origin_ancestor_term->name; if($link == true) echo '</a>'; }else{ foreach ($ancestors as $ancestor) { $ancestor_term = get_term_by("id", $ancestor, "product_cat"); $ancestor_link = get_term_link( $ancestor_term->slug, $ancestor_term->taxonomy ); if($c++ == 1) echo '» '; else if($c++ != 1 || $c++ != $ac) echo ' » '; if($link == true) echo '<a href="'. $ancestor_link .'">'; echo $ancestor_term->name; if($link == true) echo '</a>'; } $descendant_term = get_term_by("id", $descendant_id, "product_cat"); $descendant_link = get_term_link( $descendant_term->slug, $descendant_term->taxonomy ); echo ' » '; if($link == true) echo '<a href="'. $descendant_link .'">'; echo $descendant->name; if($link == true) echo '</a>'; } }
使用方法
- トップレベル、元の祖先のみ.リンクなし
wc_origin_trail_ancestor();
- トップレベル、元の祖先のみ.リンク付き
wc_origin_trail_ancestor(true);
- 祖先の道;リンクなし
wc_origin_trail_ancestor(false,true);
- 祖先の道;リンク付き
wc_origin_trail_ancestor(true,true);
メモ
- 商品に複数のメイン/トップレベルのカテゴリがある場合、または少なくともそれらすべてが表示されない場合は機能しません.
- 同じレベルの複数のサブカテゴリにも同じことが当てはまります.
- 関数はそのプロジェクトに必要なすべてを実行するため、上記の点に関してこれ以上のテストは行いませんでした.
just to offer a alternative solution that might help somebody:
code
function wc_origin_trail_ancestor( $link = false, $trail = false ) { if (is_product_category()) { global $wp_query; $q_obj = $wp_query->get_queried_object(); $cat_id = $q_obj->term_id; $descendant = get_term_by("id", $cat_id, "product_cat"); $descendant_id = $descendant->term_id; $ancestors = get_ancestors($cat_id, 'product_cat'); $ancestors = array_reverse($ancestors); $origin_ancestor = get_term_by("id", $ancestors[0], "product_cat"); $origin_ancestor_id = $origin_ancestor->term_id; $ac = count($ancestors); } else if ( is_product() ) { $descendant = get_the_terms( $post->ID, 'product_cat' ); $descendant = array_reverse($descendant); $descendant = $descendant[0]; $descendant_id = $descendant->term_id; $ancestors = array_reverse(get_ancestors($descendant_id, 'product_cat')); $ac = count($ancestors); } $c = 1; if( $trail == false ){ $origin_ancestor_term = get_term_by("id", $ancestors[0], "product_cat"); $origin_ancestor_link = get_term_link( $origin_ancestor_term->slug, $origin_ancestor_term->taxonomy ); if($link == true) echo '<a href="'. $origin_ancestor_link .'">'; echo $origin_ancestor_term->name; if($link == true) echo '</a>'; }else{ foreach ($ancestors as $ancestor) { $ancestor_term = get_term_by("id", $ancestor, "product_cat"); $ancestor_link = get_term_link( $ancestor_term->slug, $ancestor_term->taxonomy ); if($c++ == 1) echo '» '; else if($c++ != 1 || $c++ != $ac) echo ' » '; if($link == true) echo '<a href="'. $ancestor_link .'">'; echo $ancestor_term->name; if($link == true) echo '</a>'; } $descendant_term = get_term_by("id", $descendant_id, "product_cat"); $descendant_link = get_term_link( $descendant_term->slug, $descendant_term->taxonomy ); echo ' » '; if($link == true) echo '<a href="'. $descendant_link .'">'; echo $descendant->name; if($link == true) echo '</a>'; } }
how to use
- just toplevel, origin ancestor; without link
wc_origin_trail_ancestor();
- just toplevel, origin ancestor; with link
wc_origin_trail_ancestor(true);
- ancestor trail; without link
wc_origin_trail_ancestor(false,true);
- ancestor trail; with link
wc_origin_trail_ancestor(true,true);
notes
- won't work if a product has multiple main-/toplevel-categories, or at least won't show them all;
- same should be the case for multiple subcategories on the same level;
- I did no further tests concerning above points, because the function does all I wanted for that project;
-
- 2012-06-28
これを試すことができます:
<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); $parents = get_the_terms($term->parent, get_query_var('taxonomy') ); foreach( $parents as $parent ) { echo $parent->name; } ?>
You can try this:
<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); $parents = get_the_terms($term->parent, get_query_var('taxonomy') ); foreach( $parents as $parent ) { echo $parent->name; } ?>
-
3つ以上のレベルのカテゴリをどのように処理しますか?How would you handle more than two levels of categories?
- 0
- 2012-06-28
- Anriëtte Myburgh
誰か助けてくれませんか.WooCommerce製品の主な親製品カテゴリを見つける方法を探していますか?たとえば、製品はガジェットの下にマークされていますが、それらすべての主な親は電子機器です.
メインの親
product_cat
を示すクラスを各投稿に追加したいので、投稿ごとにこれが必要です.商品カテゴリはカスタム分類法であり、
get_category_parents()
を使用して取得することはできません.それらは用語としてリストされています.よろしくお願いします.
//編集:
これは私がすでに持っているコードです.各投稿でこれを呼び出しており、私の投稿はアーカイブページのようにレンダリングされます.