Load Name Taxonomy from tid

By admin, 24 July, 2018

Drupal 7
เราใช้ ฟังชั่น taxonomy_term_load()

สำหรับ tid ตัวเดียว

<?php

$term = taxonomy_term_load($tid);
$name = $term->name;

?>

สำหรับ หรับ tid  หลายตัว

<?php

$tids = array(1, 2, 3);
$terms = taxonomy_term_load_multiple($tids);

foreach ($terms as $term) {
  $name = $term->name;
}

?>

 

Drupal 8

สำหรับ tid ตัวเดียว

<?php

$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($tid);

$name = $term->label();

?>

สำหรับ หรับ tid  หลายตัว

<?php

$tids = array(1, 2, 3);

$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadMultiple($tids);

foreach($terms as $term) {
  $name = $term->label();
}

?>