¿Cómo obtener el enlace de categoría de producto de WooCommerce por ID?
3 respuestas
- votos
-
- 2014-10-03
Otra actualización (septiembre de 2015):
Después detodo,puedo usar
get_term_link
. Elproblemaera que la cadenanecesitaba convertirseen unentero. Usé un consejo de desbordamiento depila para laformamás rápida de convertirlo usandoel valor (int) $ PHP.Así que se vería así sino desea utilizarel slugen elbucleforeach:
$woo_cat_id_int = (int)$woo_cat_id; //convert
get_term_link
. Espero que ayude a alguien. :-)
aña Parece que lo descubrí.Usé get_term_link . Y recibía unerrorporque loestaba usando deestamanera:
get_term_link( $woo_cat_id, 'product_cat' );
Lo queme dioesteerror:
El objeto de la clase WP_Errorno sepudo convertiren una cadena
Así que seguíesta ruta conel
slug
yfuncionó:$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
-
Aunquetodavíanoentiendopor quénotomará laidentificaciónpero sí lababosa.El Codex dice queget_term_link deberíatomar laidentificación ...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
-
Notiene sentido: deberíafuncionar con laidentificación de hecho ...muchasgraciasMakes zero sense - should work with the id indeed... many thanks
- 1
- 2014-11-19
- akmur
-
Term_ides una cadenaen el objeto.Para usarlo con lafunciónget term link,primero debe analizarlo como unentero `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
-
La solución deforsvunnetfuncionóperfectamenteparamíThe solution by forsvunnet woked perfectly for me
- 0
- 2016-08-31
- Shane Jones
-
- 2014-12-16
Gracias,uso
foreach ( $terms as $term ) { $term_link = get_term_link( $term ); echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>'; }
Funcionaperfectamente.
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()
funciona sinproblemas cuando se usael objetodevueltoporget_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()
.
product_cat
. En unafunción queestoyescribiendo,estoy usandoget_categories
conelparámetrotaxonomy
establecidoenproduct_cat
. Todofuncionabien ypuedo obtenereltérminoid,elnombree incluso lababosa. Lo quenopuedoentenderes cómo hacer que semuestreelenlace. Aparentemente,get_category_link
nofunciona con lataxonomíapersonalizada yget_term_link
tampocofunciona,aparece unerror. Estoes lo quetengo:¿Sugerencias?