Cum să obții ID-ul categoriei curente de produse în pagina de arhivă

12 aug. 2015, 17:47:51
Vizualizări: 84.4K
Voturi: 10

Dacă mă aflu pe o pagină de arhivă a produselor, cum pot obține ID-ul categoriei curente de produse? Folosesc următorul cod, dar nu obțin niciun rezultat:

global $wp_query;
$category_name = $wp_query->query_vars['product_cat'];
$category_object = get_term_by('name', $category_name, 'product_cat');
$category_id = $category_object->term_id;
// Obține variabila globală $wp_query
// Preia numele categoriei din query_vars
// Obține obiectul categoriei folosind numele
// Extrage ID-ul categoriei din obiect
0
Toate răspunsurile la întrebare 2
2
26

Am rezolvat acest lucru pentru a obține ID-ul categoriei curente.

$cate = get_queried_object();
$cateID = $cate->term_id;
echo $cateID;

Și funcționează perfect.

12 aug. 2015 18:06:46
Comentarii

apare această eroare: Notice: Undefined property: WP_Post_Type::$term_id în file.php

sheetal sheetal
25 apr. 2018 13:35:47

@Exclutips Super! A funcționat perfect pentru mine! Mulțumesc!

Ruvee Ruvee
5 apr. 2021 05:51:24
0
-1

Funcționează perfect!

    <?php
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) {
    $product_cat_id = $term->term_id;
    if( $term = get_term_by( 'id', $product_cat_id, 'product_cat' ) ){echo $term->name;}
    break;
}?>
24 mai 2018 01:33:55