我使用的是这段代码,结果只有一个正斜杠。我正在尝试打印WooCommerce产品的父类别。
$prod_cat = get_category_parents($product->id);
echo $prod_cat;
知道如何修改它以打印父类别名称吗?我使用的是这段代码,结果只有一个正斜杠。我正在尝试打印WooCommerce产品的父类别。
$prod_cat = get_category_parents($product->id);
echo $prod_cat;
知道如何修改它以打印父类别名称吗?这个get_category_parents
是一个检索作为参数传递的父类别的函数category id.
因此,如果您有一个产品,并且希望检索附加类别的列表,那么正确的功能是get_the_terms
.该函数返回所有categories对象,没有顺序,因此您可以获取这些类别中的第一个并调用get_category_parents
.
假设$product
变量包含post对象,可以使用
$cats = get_the_terms( $product->ID, \'product_cat\' );
echo $cats ? get_category_parents( array_shift($cats)->term_id ) : \'\';
下面的代码应该做你想做的事情,它有工作的好处product
和product category
环境。此外,它还内置了一些选项,如下所述how to use
.
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);
我不希望客户端错误地停用woocommerce插件。我最近在wordpress中遇到了一个叫做必须使用插件的概念。(http://codex.wordpress.org/Must_Use_Plugins)我只是想知道是否有可能将woocommerce作为必备插件使用。