lib/Post.php
public function terms( $args = array(), $merge = true, $term_class = '' ) {
		// Ensure backwards compatibility.
		if ( ! is_array( $args ) || isset( $args[0] ) ) {
			$args = array(
				'query' => array(
					'taxonomy' => $args,
				),
				'merge' => $merge,
				'term_class' => $term_class,
			);

			if ( empty( $args['term_class']) ) {
				$args['term_class'] = $this->TermClass;
			}
		}

		// Defaults.
		$args = wp_parse_args( $args, array(
			'query' => array(
				'taxonomy' => 'all',
			),
			'merge' => true,
			'term_class' => $this->TermClass,
		) );

		$tax        = $args['query']['taxonomy'];
		$merge      = $args['merge'];
		$term_class = $args['term_class'];

		$taxonomies = array();

		// @todo: Remove in 2.x
		if ( is_string($merge) && class_exists($merge) ) {
			$term_class = $merge;
		}

		// Build an array of taxonomies.
		if ( is_array( $tax ) ) {
			$taxonomies = $tax;
		} elseif ( is_string( $tax ) ) {
			if ( in_array( $tax, array( 'all', 'any', '' ) ) ) {
				$taxonomies = get_object_taxonomies($this->post_type);
			} else {
				$taxonomies = array($tax);
			}
		}

		// @todo Remove in 2.x
		$taxonomies = array_map( function( $taxonomy ) {
			if ( in_array( $taxonomy, array( 'tag', 'tags' ), true ) ) {
				$taxonomy = 'post_tag';
			} elseif ( 'categories' === $taxonomy ) {
				$taxonomy = 'category';
			}

			return $taxonomy;
		}, $taxonomies );

		$terms = wp_get_post_terms( $this->ID, $taxonomies, $args['query'] );

		if ( is_wp_error( $terms ) ) {
			/**
			 * @var $terms \WP_Error
			 */
			Helper::error_log( "Error retrieving terms for taxonomies on a post in timber-post.php" );
			Helper::error_log( 'tax = ' . print_r( $tax, true ) );
			Helper::error_log( 'WP_Error: ' . $terms->get_error_message() );

			return $terms;
		}

		// Map over array of WordPress terms and transform them into instances of the chosen term class.
		$terms = array_map( function( $term ) use ( $term_class ) {
			return call_user_func( array( $term_class, 'from' ), $term->term_id, $term->taxonomy );
		}, $terms );

		if ( ! $merge ) {
			$terms_sorted = array();

			// Initialize sub-arrays.
			foreach ( $taxonomies as $taxonomy ) {
				$terms_sorted[ $taxonomy ] = array();
			}

			// Fill terms into arrays.
			foreach ( $terms as $term ) {
				$terms_sorted[ $term->taxonomy ][] = $term;
			}

			return $terms_sorted;
		}

		return $terms;
	}
public function terms( $args = array(), $merge = true, $term_class = '' ) {
		// Ensure backwards compatibility.
		if ( ! is_array( $args ) || isset( $args[0] ) ) {
			$args = array(
				'query' => array(
					'taxonomy' => $args,
				),
				'merge' => $merge,
				'term_class' => $term_class,
			);

			if ( empty( $args['term_class']) ) {
				$args['term_class'] = $this->TermClass;
			}
		}

		// Defaults.
		$args = wp_parse_args( $args, array(
			'query' => array(
				'taxonomy' => 'all',
			),
			'merge' => true,
			'term_class' => $this->TermClass,
		) );

		$tax        = $args['query']['taxonomy'];
		$merge      = $args['merge'];
		$term_class = $args['term_class'];

		$taxonomies = array();

		// @todo: Remove in 2.x
		if ( is_string($merge) && class_exists($merge) ) {
			$term_class = $merge;
		}

		// Build an array of taxonomies.
		if ( is_array( $tax ) ) {
			$taxonomies = $tax;
		} elseif ( is_string( $tax ) ) {
			if ( in_array( $tax, array( 'all', 'any', '' ) ) ) {
				$taxonomies = get_object_taxonomies($this->post_type);
			} else {
				$taxonomies = array($tax);
			}
		}

		// @todo Remove in 2.x
		$taxonomies = array_map( function( $taxonomy ) {
			if ( in_array( $taxonomy, array( 'tag', 'tags' ), true ) ) {
				$taxonomy = 'post_tag';
			} elseif ( 'categories' === $taxonomy ) {
				$taxonomy = 'category';
			}

			return $taxonomy;
		}, $taxonomies );

		$terms = wp_get_post_terms( $this->ID, $taxonomies, $args['query'] );

		if ( is_wp_error( $terms ) ) {
			/**
			 * @var $terms \WP_Error
			 */
			Helper::error_log( "Error retrieving terms for taxonomies on a post in timber-post.php" );
			Helper::error_log( 'tax = ' . print_r( $tax, true ) );
			Helper::error_log( 'WP_Error: ' . $terms->get_error_message() );

			return $terms;
		}

		// Map over array of WordPress terms and transform them into instances of the chosen term class.
		$terms = array_map( function( $term ) use ( $term_class ) {
			return call_user_func( array( $term_class, 'from' ), $term->term_id, $term->taxonomy );
		}, $terms );

		if ( ! $merge ) {
			$terms_sorted = array();

			// Initialize sub-arrays.
			foreach ( $taxonomies as $taxonomy ) {
				$terms_sorted[ $taxonomy ] = array();
			}

			// Fill terms into arrays.
			foreach ( $terms as $term ) {
				$terms_sorted[ $term->taxonomy ][] = $term;
			}

			return $terms_sorted;
		}

		return $terms;
	}
public function terms ( $ args = array ( ) , $ merge = true , $ term_class = '' ) { // Ensure backwards compatibility. if ( ! is_array ( $ args ) || isset ( $ args [ 0 ] ) ) { $ args = array ( 'query' => array ( 'taxonomy' => $ args , ) , 'merge' => $ merge , 'term_class' => $ term_class , ) ; if ( empty ( $ args [ 'term_class' ] ) ) { $ args [ 'term_class' ] = $ this -> TermClass ; } } // Defaults. $ args = wp_parse_args ( $ args , array ( 'query' => array ( 'taxonomy' => 'all' , ) , 'merge' => true , 'term_class' => $ this -> TermClass , ) ) ; $ tax = $ args [ 'query' ] [ 'taxonomy' ] ; $ merge = $ args [ 'merge' ] ; $ term_class = $ args [ 'term_class' ] ; $ taxonomies = array ( ) ; // @todo: Remove in 2.x if ( is_string ( $ merge ) && class_exists ( $ merge ) ) { $ term_class = $ merge ; } // Build an array of taxonomies. if ( is_array ( $ tax ) ) { $ taxonomies = $ tax ; } elseif ( is_string ( $ tax ) ) { if ( in_array ( $ tax , array ( 'all' , 'any' , '' ) ) ) { $ taxonomies = get_object_taxonomies ( $ this -> post_type ) ; } else { $ taxonomies = array ( $ tax ) ; } } // @todo Remove in 2.x $ taxonomies = array_map ( function ( $ taxonomy ) { if ( in_array ( $ taxonomy , array ( 'tag' , 'tags' ) , true ) ) { $ taxonomy = 'post_tag' ; } elseif ( 'categories' === $ taxonomy ) { $ taxonomy = 'category' ; } return $ taxonomy ; } , $ taxonomies ) ; $ terms = wp_get_post_terms ( $ this -> ID , $ taxonomies , $ args [ 'query' ] ) ; if ( is_wp_error ( $ terms ) ) { /**
			 * @var $terms \WP_Error
			 */ Helper :: error_log ( "Error retrieving terms for taxonomies on a post in timber-post.php" ) ; Helper :: error_log ( 'tax = ' . print_r ( $ tax , true ) ) ; Helper :: error_log ( 'WP_Error: ' . $ terms -> get_error_message ( ) ) ; return $ terms ; } // Map over array of WordPress terms and transform them into instances of the chosen term class. $ terms = array_map ( function ( $ term ) use ( $ term_class ) { return call_user_func ( array ( $ term_class , 'from' ) , $ term -> term_id , $ term -> taxonomy ) ; } , $ terms ) ; if ( ! $ merge ) { $ terms_sorted = array ( ) ; // Initialize sub-arrays. foreach ( $ taxonomies as $ taxonomy ) { $ terms_sorted [ $ taxonomy ] = array ( ) ; } // Fill terms into arrays. foreach ( $ terms as $ term ) { $ terms_sorted [ $ term -> taxonomy ] [ ] = $ term ; } return $ terms_sorted ; } return $ terms ; }
Gets the terms associated with the post.

@api
@todo Remove deprecated parameters in 2.x
@example
```twig
<section id="job-feed">
{% for post in job %}
<div class="job">
<h2>{{ post.title }}</h2>
<p>{{ post.terms('category')|join(', ') }}</p>
</div>
{% endfor %}
</section>
```
```html
<section id="job-feed">
<div class="job">
<h2>Cheese Maker</h2>
<p>Food, Cheese, Fromage</p>
</div>
<div class="job">
<h2>Mime</h2>
<p>Performance, Silence</p>
</div>
</section>
```
```php
// Get all terms of a taxonomy.
$terms = $post->terms( 'category' );

// Get terms of multiple taxonomies.
$terms = $post->terms( array( 'books', 'movies' ) );

// Use custom arguments for taxonomy query and options.
$terms = $post->terms( array(
'query' => [
'taxonomy' => 'custom_tax',
'orderby'  => 'count',
],
'merge'      => false,
'term_class' => 'My_Term_Class'
) );
```

@param string|array $args {
Optional. Name of the taxonomy or array of arguments.

@type array $query       Any array of term query parameters for getting the terms. See
`WP_Term_Query::__construct()` for supported arguments. Use the
`taxonomy` argument to choose which taxonomies to get. Defaults
to querying all registered taxonomies for the post type. You can
use custom or built-in WordPress taxonomies (category, tag).
Timber plays nice and figures out that `tag`, `tags` or
`post_tag` are all the same (also for `categories` or
`category`). For custom taxonomies you need to define the
proper name.
@type bool $merge        Whether the resulting array should be one big one (`true`) or
whether it should be an array of sub-arrays for each taxonomy
(`false`). Default `true`.
@type string $term_class The Timber term class to use for the term objects.
}
@param bool   $merge      Deprecated. Optional. See `$merge` argument in `$args` parameter.
@param string $term_class Deprecated. Optional. See `$term_class` argument in `$args`
parameter.
@return array An array of taxonomies.
Gets the terms associated with the post .