get_the_terms() - WordPress 函数参考中文文档

返回介绍

get_the_terms()

发布于 2017-09-11 字数6121 浏览 893 评论 0

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

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


描述


参数

$post

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

$taxonomy

(string)
(Required)
Taxonomy name.


返回值

(array|false|WP_Error) Array of WP_Term objects on success, false if there are no terms or the post does not exist, WP_Error on failure.


源代码

File: wp-includes/category-template.php

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

	$terms = get_object_term_cache( $post->ID, $taxonomy );
	if ( false === $terms ) {
		$terms = wp_get_object_terms( $post->ID, $taxonomy );
		if ( ! is_wp_error( $terms ) ) {
			$term_ids = wp_list_pluck( $terms, 'term_id' );
			wp_cache_add( $post->ID, $term_ids, $taxonomy . '_relationships' );
		}
	}

	/**
	 * Filters the list of terms attached to the given post.
	 *
	 * @since 3.1.0
	 *
	 * @param array|WP_Error $terms    List of attached terms, or WP_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;
}

更新日志

Version 描述
2.5.0 Introduced.

相关函数

Uses

  • wp-includes/cache.php:
    wp_cache_add()
  • wp-includes/category-template.php:
    get_the_terms
  • wp-includes/functions.php:
    wp_list_pluck()
  • wp-includes/taxonomy.php:
    get_object_term_cache()
  • wp-includes/taxonomy.php:
    wp_get_object_terms()
  • wp-includes/plugin.php:
    apply_filters()
  • wp-includes/post.php:
    get_post()
  • wp-includes/load.php:
    is_wp_error()
  • Show 3 more uses
    Hide more uses


Used By

  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:
    WP_REST_Posts_Controller::prepare_item_for_response()
  • wp-admin/includes/class-wp-posts-list-table.php:
    WP_Posts_List_Table::column_default()
  • wp-admin/includes/class-wp-media-list-table.php:
    WP_Media_List_Table::column_default()
  • wp-includes/category-template.php:
    get_the_term_list()
  • wp-includes/category-template.php:
    get_the_tags()
  • wp-includes/category-template.php:
    get_the_category()
  • wp-includes/post-template.php:
    get_post_class()
  • wp-includes/class-wp-post.php:
    WP_Post::__get()
  • wp-includes/post-formats.php:
    get_post_format()
  • Show 4 more used by
    Hide more used by


User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 16You must log in to vote on the helpfulness of this note

    Contributed by mklusak

    WP_Term object properties: (because I am always looking for them)

    WP_Term Object
    (
    [term_id] =>
    [name] =>
    [slug] =>
    [term_group] =>
    [term_taxonomy_id] =>
    [taxonomy] =>
    [描述] =>
    [parent] =>
    [count] =>
    [filter] =>
    )

  2. A Basic Example

    Echoing the list of terms (for a taxonomy called on-draught). This is similar to the output from get_the_term_list, but without the terms being hyperlinked:

    
    $terms = get_the_terms( get_the_ID(), 'on-draught' );
    
    if ( $terms && ! is_wp_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 terms for all custom taxonomies

    Place this function in your theme’s functions.php.

    
    /**
     * Get taxonomies terms links.
     *
     * @see get_object_taxonomies()
     */
    function wpdocs_custom_taxonomies_terms_links() {
    	// Get post by post ID.
    	$post = get_post( $post->ID );
    
    	// 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 相关函数 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="%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 );
    }
    ?>
    

    Now you can use this function in your theme:

    
    <?php echo wpdocs_custom_taxonomies_terms_links(); ?>
    

    The function returns WP_Error if the $taxonomy doesn’t exist.

上一篇:get_the_taxonomies()

下一篇:get_the_term_list()

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!