获取产品的主要父类别

时间:2012-06-28 作者:Anriëtte Myburgh

有人能帮助我吗?我正在寻找一种方法来找到WooCommerce产品的主要母产品类别?比如说,该产品被标记在小工具下,但它们的主要母产品都是电子产品。

我希望每个帖子都有这样的内容,因为我想在每个帖子中添加一个类,表示其主要父级product_cat.

请记住,产品类别是自定义分类法,无法使用get_category_parents(). 它们列为术语。

提前谢谢。

//编辑:

这是我已经拥有的代码,我在每篇帖子上调用它,我的帖子呈现为类似于归档页面。

function all_cat_classes($post) {
    $cats = "";

    $terms = get_the_terms($post->ID, "product_cat");

    $count = 0;
    $count = count($terms);
    $key = 0;
    foreach ($terms as $cat) {

        $key++;
    }

    return $cats;
}

3 个回复
最合适的回答,由SO网友:Anriëtte Myburgh 整理而成

我编写了自己的函数,以沿着“链”一直向上。我的递归可能不是您见过的最好的实现,但它很有效。

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;
}

SO网友:Nicolai Grossherr

只是为了提供一种可能对某人有所帮助的替代解决方案:

代码

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);

    注释将不起作用

SO网友:Pontus Abrahamsson

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;
  }

?>
结束

相关推荐

以编程方式添加TERMS/TERM_TASTIONIES时出现异常行为

这在我看来确实像个虫子。我为您编写了以下代码,以便在新安装上对其进行测试。基本上,代码将在“未分类”下添加两个子类别,以实现以下效果uncategorized sub-uncategorized sub-sub-uncategorized 将它们添加到wp\\U terms和wp\\U term\\u分类中没有问题。已正确添加两个父ID。问题出在wp管理ui中。您只能看到未分类和子未分类。最后一个(sub-sub)在运行中丢失!但是,如果您在同一个“添加类别”