当前浏览:首页 / WordPress函数 / get_the_terms()

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

检索附加到文章的分类法项目

term 分类项more...

themore...


参数

$post

(int|WP_Post) (必填) 文章 ID或对象

$taxonomy

(string) (必填) 分类法名称(taxonomy name)


返回

(WP_Term[]|false|WP_Error) 成功返回WP_Term对象数组,如果没有项目或文章不存在则返回false,失败返回WP_Error



源码

查看源码 官方文档


更新日志

版本描述
2.5.0开始引入

使用示例

  • 示例1

    WP_Term对象属性:(因为我一直在寻找它们)

    WP_Term Object
    (
    	[term_id] => 
    	[name] => 
    	[slug] => 
    	[term_group] => 
    	[term_taxonomy_id] => 
    	[taxonomy] => 
    	[description] => 
    	[parent] => 
    	[count] => 
    	[filter] => 
    )
    
  • 示例2

    获得逗号分隔的项目列表的优化方法。

    $term_obj_list = get_the_terms( $post->ID, 'taxonomy' );
    $terms_string = join(', ', wp_list_pluck($term_obj_list, 'name'));
    
  • 示例3

    获取所有自定义分类法的项目

    将此功能放在主题的functions.php中。

    /**
     * Get taxonomies terms links.
     *
     * @see get_object_taxonomies()
     */
    function wpdocs_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="%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 );
    }
    ?>
    

    现在,您可以在主题中使用此功能:

    <?php echo wpdocs_custom_taxonomies_terms_links(); ?>
    
  • 示例4

    基本示例

    输出项目列表(分类法为on-draught)。这与get_the_term_list的输出类似,但没有超链接:

    $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; ?>
    
  • 示例5

    此函数与wp_get_post_terms()之间的区别在于,此函数的结果是缓存的(因此,它不会每次调用时都命中数据库)。

  • 示例6

    如何处理此调用结果的另一个示例。

    此示例检索文章的类别和标签,并使用wp_list_pluck()高效地创建其名称列表,无重复项,并将其转换为适合NITF提要的键字符串。

    		// Get a list of categories and extract their names
    		$post_categories = get_the_terms( $post->ID, 'category' );
    		if ( ! empty( $post_categories ) && ! is_wp_error( $post_categories ) ) {
    			$categories = wp_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_wp_error( $post_tags ) ) {
    			$tags = wp_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;
    
    
  • 示例7

    get_the_terms()的示例,其中包含附加到文章的项目返回列表

    if ( ! function_exists( 'wpdocs_example_get_the_terms' ) ) {
    
        /**
         * Function to return list of the terms.
         * 
         * @param string 'taxonomy'
         * 
         * @return html Returns the list of elements.
         */
    
        function wpdocs_example_get_the_terms( $taxonomy ) {
            
            $terms = get_the_terms( get_the_ID(), $taxonomy );
                             
            if ( $terms && ! is_wp_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;
    
        }
    
    }

    如何使用:

    wpdocs_example_get_the_terms( 'taxonomy' )
  • 示例8

    另一个示例是如何获取自定义文章类型并用逗号分隔它们。

    $terms = get_the_terms( $post->ID , array( 'teams_positions') );
    // init counter
    $i = 1;
    foreach ( $terms as $term ) {
     $term_link = get_term_link( $term, array( 'teams_positions') );
     if( is_wp_error( $term_link ) )
     continue;
     echo $term->name;
     //  Add comma (except after the last theme)
     echo ($i < count($terms))? " / " : "";
     // Increment counter
     $i++;
    }
  • 示例9

    如果要使用内部WP 查询,只需列出分类名称,则可以使用:

    <ul>
    foreach ( get_the_terms( get_the_ID(), 'taxonomy_name' ) as $tax ) {
        echo '<li>' . __( $tax->name ) . '</li>';
    }
    </ul>