Come ottenere il Link della Categoria Prodotto WooCommerce tramite ID?
Le categorie prodotto di WooCommerce sono una tassonomia personalizzata chiamata product_cat
. In una funzione che sto scrivendo, sto usando get_categories
con il parametro taxonomy
impostato su product_cat
. Tutto funziona bene e riesco a ottenere l'ID del termine, il nome e anche lo slug. Quello che non riesco a capire è come ottenere il link da visualizzare. A quanto pare get_category_link
non funziona con le tassonomie personalizzate e get_term_link
non funziona neanche, ricevo un errore. Ecco quello che ho:
$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; //ID categoria
$woo_cat_name = $woo_cat->name; //nome categoria
$return .= '<a href="' . get_category_link( $woo_cat_id ) . '">' . $woo_cat_name . '</a>';
}//fine del foreach $woo_categories
Suggerimenti?
Un altro aggiornamento (sett. 2015):
Alla fine posso usare get_term_link
. Il problema era che la stringa doveva essere convertita in un intero. Ho utilizzato un suggerimento da Stack Overflow per il modo più veloce per convertirlo usando (int)$value in PHP.
Quindi apparirebbe così se non vuoi usare lo slug nel ciclo foreach:
$woo_cat_id_int = (int)$woo_cat_id; //conversione
Questo valore convertito viene utilizzato al posto dello slug in get_term_link
. Spero possa essere utile a qualcuno. :-)
Sembra che l'abbia risolto.
Ho usato get_term_link. E ricevevo un errore perché lo stavo usando in questo modo:
get_term_link( $woo_cat_id, 'product_cat' );
Che mi dava questo errore:
L'oggetto della classe WP_Error non può essere convertito in stringa
Quindi ho seguito questa strada invece con lo slug
e ha funzionato:
$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; //ID categoria
$woo_cat_name = $woo_cat->name; //nome categoria
$woo_cat_slug = $woo_cat->slug; //slug categoria
$return .= '<a href="' . get_term_link( $woo_cat_slug, 'product_cat' ) . '">' . $woo_cat_name . '</a>';
}//fine del foreach $woo_categories

Anche se ancora non capisco perché non accetti l'ID ma accetta lo slug. Il Codex dice che get_term_link dovrebbe accettare l'ID...

Non ha alcun senso - dovrebbe funzionare con l'id infatti... grazie mille

Term_id è una stringa nell'oggetto. Per usarlo con la funzione get_term_link devi prima convertirlo in intero get_term_link( intval($woo_cat->term_id), 'product_cat' )

$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()
funziona correttamente quando viene utilizzato l'oggetto restituito da get_categories()
.

Ho provato questo codice e funziona.
<?php $catTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'exclude' => '17,77')); ?>
<?php foreach($catTerms as $catTerm) :
// Ottiene l'ID dell'immagine in miniatura per la categoria
$wthumbnail_id = get_woocommerce_term_meta( $catTerm->term_id,'thumbnail_id', true );
// Recupera l'URL dell'immagine
$wimage = wp_get_attachment_url( $wthumbnail_id );
// Ottiene il link alla pagina della categoria
$wcategory =$term_link = get_term_link( $catTerm );
?>
<ul>
<li><a class="tr" href="<?php echo esc_url( $term_link ); ?>"><?php if($wimage!=""):?><img src="<?php echo $wimage?>" alt="<?php echo esc_attr($catTerm->name); ?>" title="<?php echo esc_attr($catTerm->name); ?>"><?php endif;?><?php echo $catTerm->name; ?></a></li>
</ul>
