^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_the_terms()用法示例

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.

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包

$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


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


  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.

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈