get_the_terms()

最后更新于:2021-11-27 02:49:27

get_the_terms( int|GC_Post$post, string$taxonomy)

Retrieves the terms of the taxonomy that are attached to the post.

参数

$post

(int|GC_Post) (Required) Post ID or object.

$taxonomy

(string) (Required) Taxonomy name.

响应

(GC_Term[]|false|GC_Error) Array of GC_Term objects on success, false if there are no terms or the post does not exist, GC_Error on failure.

源文件

文件: gc-includes/category-template.php

function get_the_terms( $post, $taxonomy ) {
	$post = get_post( $post );
	if ( ! $post ) {
		return false;
	}

	$terms = get_object_term_cache( $post->ID, $taxonomy );
	if ( false === $terms ) {
		$terms = gc_get_object_terms( $post->ID, $taxonomy );
		if ( ! is_gc_error( $terms ) ) {
			$term_ids = gc_list_pluck( $terms, 'term_id' );
			gc_cache_add( $post->ID, $term_ids, $taxonomy . '_relationships' );
		}
	}

	/**
	 * Filters the list of terms attached to the given post.
	 *
	 * @since 3.1.0
	 *
	 * @param GC_Term[]|GC_Error $terms    Array of attached terms, or GC_Error on failure.
	 * @param int                $post_id  Post ID.
	 * @param string             $taxonomy Name of the taxonomy.
	 */
	$terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy );

	if ( empty( $terms ) ) {
		return false;
	}

	return $terms;
}
/**
 * Get taxonomies terms links.
 *
 * @see get_object_taxonomies()
 */
function gcdocs_custom_taxonomies_terms_links() {
	// Get post by post ID.
	if ( ! $post = get_post() ) {
		return '';
	}

	// Get post type by post.
	$post_type = $post->post_type;

	// Get post type taxonomies.
	$taxonomies = get_object_taxonomies( $post_type, 'objects' );

	$out = array();

	foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){

		// Get the terms related to post.
		$terms = get_the_terms( $post->ID, $taxonomy_slug );

		if ( ! empty( $terms ) ) {
			$out[] = "<h2>" . $taxonomy->label . "</h2>n<ul>";
			foreach ( $terms as $term ) {
				$out[] = sprintf( '<li><a href="https://docs.gechiui.com/functions/get_the_terms/%1$s">%2$s</a></li>',
					esc_url( get_term_link( $term->slug, $taxonomy_slug ) ),
					esc_html( $term->name )
				);
			}
			$out[] = "n</ul>n";
		}
	}
	return implode( '', $out );
}
?>
$terms = get_the_terms( get_the_ID(), 'on-draught' );
						
if ( $terms && ! is_gc_error( $terms ) ) : 

	$draught_links = array();

	foreach ( $terms as $term ) {
		$draught_links[] = $term->name;
	}
						
	$on_draught = join( ", ", $draught_links );
	?>

	<p class="beers draught">
		<?php printf( esc_html__( 'On draught: <span>%s</span>', 'textdomain' ), esc_html( $on_draught ) ); ?>
	</p>
<?php endif; ?>
		// Get a list of categories and extract their names
		$post_categories = get_the_terms( $post->ID, 'category' );
		if ( ! empty( $post_categories ) && ! is_gc_error( $post_categories ) ) {
			$categories = gc_list_pluck( $post_categories, 'name' );
		}

		// Get a list of tags and extract their names
		$post_tags = get_the_terms( $post->ID, 'post_tag' );
		if ( ! empty( $post_tags ) && ! is_gc_error( $post_tags ) ) {
			$tags = gc_list_pluck( $post_tags, 'name' );
		}

		// Combine Categories and tags, with category first
		$tagCats = array_merge( $categories, $tags );

		// Process to desired format
		$keyList = '<key-list>' . PHP_EOL .
			'	<keyword key="' . 
					implode( $tagCats, '"/>' . PHP_EOL .	'	<keyword key="') . 
				'"/>' . PHP_EOL .
			'</key-list>' . PHP_EOL;

if ( ! function_exists( 'gcdocs_example_get_the_terms' ) ) {

    /**
     * Function to return list of the terms.
     * 
     * @param string 'taxonomy'
     * 
     * @return html 响应s the list of elements.
     */

    function gcdocs_example_get_the_terms( $taxonomy ) {
        
        $terms = get_the_terms( get_the_ID(), $taxonomy );
                         
        if ( $terms && ! is_gc_error( $terms ) ) : 
        
            $term_links = array();
        
            foreach ( $terms as $term ) {
                $term_links[] = '<a href="' . esc_attr( get_term_link( $term->slug, $taxonomy ) ) . '">' . __( $term->name ) . '</a>';
            }
                                
            $all_terms = join( ', ', $term_links );

            echo '<span class="terms-' . esc_attr( $term->slug ) . '">' . __( $all_terms ) . '</span>';

        endif;

    }

}