WordPress函数get_the_terms()检索帖子附加的分类术语

WordPress函数get_the_terms()检索帖子附加的分类术语

WordPress函数get_the_terms()检索帖子附加的分类术语

  WordPress 中的函数get_the_terms()检索与特定对象(例如帖子、页面或自定义帖子类型)关联的术语。它需要两个参数:$object$taxonomy

  推荐:[最新版]MasterStudy LMS PRO下载WordPress在线教育学习管理插件

get_the_terms()函数基本语法

描述

  确定分类对象是否分层

用法

get_the_terms($post, $taxonomy);

参数

  • $post(int|WP_Post) – 必需。帖子 ID 或对象。
  • $taxonomy(字符串)——必填。分类名称。

  用法示例:

$categories = get_the_terms($post->ID, 'category');

  推荐:[最新版]TranslatePress Pro插件下载WordPress翻译插件

get_the_terms()函数

  get_the_terms()函数是一个多功能工具,用于检索与 WordPress 中的对象关联的术语的信息。它可用于显示术语、过滤内容和执行其他任务。(源文件可参考这里

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 = 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 WP_Term[]|WP_Error $terms    Array 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;
}

  推荐:[最新版]LearnDash LMS插件WordPress学习管理系统插件

如何使用get_the_terms()

  显示帖子的类别,此示例检索帖子的类别并将其显示为列表。

$categories = get_the_terms($post->ID, 'category');
if ($categories && !is_wp_error($categories)) {
    echo '<ul>';
    foreach ($categories as $category) {
        echo '<li>' . $category->name . '</li>';
    }
    echo '</ul>';
}

  显示帖子的自定义分类术语,此示例检索名为的自定义分类法的术语genre,并将它们显示为逗号分隔的列表。

$genres = get_the_terms($post->ID, 'genre');
if ($genres && !is_wp_error($genres)) {
    $genre_names = wp_list_pluck($genres, 'name');
    echo implode(', ', $genre_names);
}

  显示帖子附加的所有分类法的术语,此示例检索附加到帖子的所有分类法的术语,并将它们显示在单独的列表中。

$taxonomies = get_object_taxonomies($post->post_type, 'objects');
foreach ($taxonomies as $taxonomy_slug => $taxonomy) {
    $terms = get_the_terms($post->ID, $taxonomy_slug);
    if ($terms && !is_wp_error($terms)) {
        echo '<h2>' . $taxonomy->label . '</h2><ul>';
        foreach ($terms as $term) {
            echo '<li>' . $term->name . '</li>';
        }
        echo '</ul>';
    }
}

  显示术语名称及其链接,此示例检索帖子的类别并将其显示为一个列表,其中包含指向其各自存档页面的链接。

$categories = get_the_terms($post->ID, 'category');
if ($categories && !is_wp_error($categories)) {
    echo '<ul>';
    foreach ($categories as $category) {
        echo '<li><a href="' . get_term_link($category) . '">' . $category->name . '</a></li>';
    }
    echo '</ul>';
}

  使用自定义HTML显示术语名称,此示例检索帖子的标签并使用自定义HTML显示它们。

$tags = get_the_terms($post->ID, 'post_tag');
if ($tags && !is_wp_error($tags)) {
    echo '<div class="tags">';
    foreach ($tags as $tag) {
        echo '<span class="tag">' . $tag->name . '</span>';
    }
    echo '</div>';
}

  推荐:WordPress函数get_theme_support()获取注册支持时传递的主题参数

  推荐:WordPress函数使用手册


晓得博客,版权所有丨如未注明,均为原创
晓得博客 » WordPress函数get_the_terms()检索帖子附加的分类术语

转载请保留链接:https://www.pythonthree.com/wordpress-get-the-terms/

滚动至顶部