---
description: https://fcwordpress.net/cac-function-lien-quan-den-term-trong-wordpress.html
---

# Các Function Liên Quan Đến Term Trong Wordpress  (ok)

Hôm nay tôi sẽ chia sẽ với anh/chị bài viết “Các function liên quan đến term trong wordpress”. Chúng ta trong quá trình làm theme thì sẽ gặp vấn đề như tôi nói dưới đây:

## **Get id term trong taxonomy template**

```
global $wp_query;
$term_id = $wp_query->get_queried_object();
$term_id->term_id
```

## **Tôi muốn lấy list term**

```
<?php
// mảng taxonomies
$taxonomies = array(
  'post_tag',
  'my_tax',
);

$args = array(
  'orderby'                => 'name',
  'order'                  => 'ASC',
  'hide_empty'             => true,
  'include'                => array(),
  'exclude'                => array(),
  'exclude_tree'           => array(),
  'number'                 => '',
  'offset'                 => '',
  'fields'                 => 'all',
  'name'                   => '',
  'slug'                   => '',
  'hierarchical'           => true,
  'search'                 => '',
  'name__like'             => '',
  'description__like'      => '',
  'pad_counts'             => false,
  'get'                    => '',
  'child_of'               => 0,
  'parent'                 => '',
  'childless'              => false,
  'cache_domain'           => 'core',
  'update_term_meta_cache' => true,
  'meta_query'             => '',
);

$terms = get_terms($taxonomies, $args);
?>
```

## **Lấy link term**

```
get_term_link( (int) $term_id,'product_cat')
```

## Lấy thông tin term từ term\_id

```
$term = get_term($idpage1,'product_cat')
$term->name;
```

## Lấy thông tin term từ post id

```
$singer = get_the_terms( get_the_ID(), "product_singer" );
<?php foreach ($singer as $singer): ?>
  <h1><?php echo $singer->name; ?></h1>
<?php endforeach;?>
$singer = get_the_terms( get_the_ID(), "product_singer" );
<?php foreach ($singer as $singer): ?>
  <h1><?php echo $singer->name; ?></h1>
<?php endforeach;?>
```

## Get id term parent từ id term child

```
<?php get_ancestors($object_id, $object_type);?>
<?php get_ancestors(208, 'category');?>
returns:
Array (
  [0] => 23
  [1] => 6
)
<?php get_ancestors($object_id, $object_type);?>
<?php get_ancestors(208, 'category');?>
returns:
Array (
  [0] => 23
  [1] => 6
)
```

## Get id term child từ id term parent

```
<?php
$term_id = 10;
$taxonomy_name = 'products';
$termchildren = get_term_children( $term_id, $taxonomy_name );
 
echo '<ul>';
foreach ( $termchildren as $child ) {
    $term = get_term_by( 'id', $child, $taxonomy_name );
    echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a></li>';
}
echo '</ul>';
?> 
```

