如何通过ID获取WooCommerce产品类别链接?
3 个回答
- 投票数
-
- 2014-10-03
另一个更新(2015年9月):
我可以毕竟使用
get_term_link
.问题在于该字符串需要转换为整数.使用 Stack Overflow提示,以最快的方式使用(int)$ PHP.因此,如果您不想在foreach循环中使用该子弹,它将看起来像这样:
$woo_cat_id_int = (int)$woo_cat_id; //convert
使用该转换后的值代替
get_term_link
中的数据段.希望它能帮助某人. :-)
好像我想通了.
我使用了get_term_link .而且我因为使用这种方式而报错:
get_term_link( $woo_cat_id, 'product_cat' );
哪个给我这个错误:
WP_Error类的对象无法转换为字符串
因此,我改用
slug
走这条路线,并且有效:$prod_cat_args = array( 'taxonomy' => 'product_cat', //woocommerce 'orderby' => 'name', 'empty' => 0 ); $woo_categories = get_categories( $prod_cat_args ); foreach ( $woo_categories as $woo_cat ) { $woo_cat_id = $woo_cat->term_id; //category ID $woo_cat_name = $woo_cat->name; //category name $woo_cat_slug = $woo_cat->slug; //category slug $return .= '<a href="' . get_term_link( $woo_cat_slug, 'product_cat' ) . '">' . $woo_cat_name . '</a>'; }//end of $woo_categories foreach
Another update (Sept. 2015):
I can use
get_term_link
after all. The problem was that the string needed to be converted to an integer. Used a Stack Overflow tip for the fastest way to convert it using the (int)$value in PHP.So it would look like this if you don't want to use the slug in the foreach loop:
$woo_cat_id_int = (int)$woo_cat_id; //convert
That converted value is used instead of the slug in
get_term_link
. Hope it helps someone. :-)
Looks like I figured it out.
I used get_term_link. And I was getting an error because I was using it this way:
get_term_link( $woo_cat_id, 'product_cat' );
Which gave me this error:
Object of class WP_Error could not be converted to string
So I went this route instead with the
slug
and it worked:$prod_cat_args = array( 'taxonomy' => 'product_cat', //woocommerce 'orderby' => 'name', 'empty' => 0 ); $woo_categories = get_categories( $prod_cat_args ); foreach ( $woo_categories as $woo_cat ) { $woo_cat_id = $woo_cat->term_id; //category ID $woo_cat_name = $woo_cat->name; //category name $woo_cat_slug = $woo_cat->slug; //category slug $return .= '<a href="' . get_term_link( $woo_cat_slug, 'product_cat' ) . '">' . $woo_cat_name . '</a>'; }//end of $woo_categories foreach
-
虽然我仍然不明白为什么它不带ID,但带子.食品法典委员会说get_term_link应该带有ID ...Although I still don't understand why it won't take the ID but it takes the slug. The Codex says get_term_link should take the ID...
- 1
- 2014-10-03
- RachieVee
-
零意义-实际上应该与id一起使用...非常感谢Makes zero sense - should work with the id indeed... many thanks
- 1
- 2014-11-19
- akmur
-
Term_id是对象上的字符串.要将其与getterm链接功能一起使用,您需要将其解析为整数,首先是"get_term_link(intval($ woo_cat->term_id),'product_cat')".Term_id is a string on the object. To use it with the get term link function you need to parse it as an integer first `get_term_link( intval($woo_cat->term_id), 'product_cat' )`
- 3
- 2015-01-28
- forsvunnet
-
forsvunnet的解决方案对我来说完全正常The solution by forsvunnet woked perfectly for me
- 0
- 2016-08-31
- Shane Jones
-
- 2014-12-16
谢谢,我使用
foreach ( $terms as $term ) { $term_link = get_term_link( $term ); echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>'; }
它运行完美.
Thanks, I use
foreach ( $terms as $term ) { $term_link = get_term_link( $term ); echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>'; }
It works perfectly.
-
- 2015-09-18
$prod_cat_args = array( 'taxonomy' => 'product_cat', //woocommerce 'orderby' => 'name', 'empty' => 0 ); $terms = get_categories( $prod_cat_args ); //$term_id=6; foreach ( $terms as $term ) { $term_link = get_term_link( $term ); echo '<li><a class="shopping-now" href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>'; }
get_term_link()
可以正常工作由get_categories()
返回.$prod_cat_args = array( 'taxonomy' => 'product_cat', //woocommerce 'orderby' => 'name', 'empty' => 0 ); $terms = get_categories( $prod_cat_args ); //$term_id=6; foreach ( $terms as $term ) { $term_link = get_term_link( $term ); echo '<li><a class="shopping-now" href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>'; }
get_term_link()
does work smoothly, when using the object returned byget_categories()
.
WooCommerce的产品类别是一种称为
product_cat
的自定义分类法.在编写的函数中,我使用的是get_categories
,并且taxonomy
参数设置为product_cat
.一切正常,我可以得到术语ID,名称甚至是子弹.我不知道是如何显示链接.显然,get_category_link
不适用于自定义分类法,并且get_term_link
也不适用,我得到了一个错误.这就是我所拥有的:建议?