Hallo, ich muss die Haupt- und Nebentaxonomie eines Produkts im Woocommerce ermitteln. Ich weiß, wie man Taxonomiedetails erhält .123 is product id .
$terms = get_the_terms(123, 'product_cat');
Ich weiß auch, wie man Taxonomienamen bekommt
echo $terms[0]->name
Aber mein Bedürfnis ist: Nokia 3110 ist mein Handy. So steht es unter electronics-> mobiles -->ordinary-->Single sim-->nokia 3110
id is 123 .
in der Produktbearbeitungsseite auf dem Admin-Panel befindet sich Nokia 3110 unter einer einzelnen SIM-Karte [tatsächlich befindet sich eine einzelne SIM-Karte unter electronics-> mobiles -> ordinary ]. gewöhnliches etc.
Mein Bedarf ist von dieser Identifikation, die ich hauptsächlich erhalten muss, in der Taxonmy Nokia 31110 ist. Die Antwort lautet: Es ist in der Elektronik. Und ich brauche auch die zweite Taxonomie, das heißt, ich muss sie unter Handys drucken. Wie bekomme ich diese Details?
Vielen Dank .
ich weiß, wie man die höchste Taxonomie erreicht
function get_term_top_most_parent($term_id, $taxonomy){
// start from the current term
$parent = get_term_by( 'id', $term_id, $taxonomy);
// climb up the hierarchy until we reach a term with parent = '0'
while ($parent->parent != '0'){
$term_id = $parent->parent;
$parent = get_term_by( 'id', $term_id, $taxonomy);
}
return $parent;
}
aber bitte helfen Sie, die zweite Top-Taxonomie zu finden.
Basierend auf dieser Antwort ist hier meine Funktion, um alle Ihre Begriffe in einem Array abzurufen:
function get_term_ancestors($post_id, $taxonomy){
// Declare the array where we are going to store the terms
$ancestors = array();
// start from the current term
$parent = array_shift(get_the_terms($post_id,$taxonomy));
// climb up the hierarchy until we reach a term with parent = '0'
while ($parent->parent != '0'){
$term_id = $parent->parent;
$parent = get_term_by( 'id', $term_id, $taxonomy);
$ancestors[] = $parent;
}
return $ancestors;
}
Verwenden Sie wie folgt:
$ancestors = get_term_ancestors(123,'product_cat');
Mit dieser Funktion lautet Ihr oberster Begriff also:
$ancestors[count($ancestors)-1] // or array_pop($ancestors);
und dein zweites übergeordnetes Level wird
$ancestors[count($ancestors)-2] // or another array_pop($ancestors)
Bitte beachten Sie auch, dass Sie isset ($ ancestors [count ($ ancestors) -2]) überprüfen sollten, bevor Sie versuchen, darauf zuzugreifen
Warnung: Dies funktioniert nur für Produkte, die sich in einem Begriff befinden. Diese Funktion behandelt nicht mehrere Begriffe