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

返回介绍

add_term_meta()

发布于 2017-09-10 字数2911 浏览 1255 评论 0

add_term_meta( int $term_id,  string $meta_key,  mixed $meta_value,  bool $unique = false )

Adds metadata to a term.


描述


参数

$term_id

(int)
(Required)
Term ID.

$meta_key

(string)
(Required)
Metadata name.

$meta_value

(mixed)
(Required)
Metadata value.

$unique

(bool)
(Optional)
Whether to bail if an entry with the same key is found for the term.

Default value: false


返回值

(int|WP_Error|bool) Meta ID on success. WP_Error when term_id is ambiguous between taxonomies. False on failure.


源代码

File: wp-includes/taxonomy.php

function add_term_meta( $term_id, $meta_key, $meta_value, $unique = false ) {
	// Bail if term meta table is not installed.
	if ( get_option( 'db_version' ) < 34370 ) {
		return false;
	}

	if ( wp_term_is_shared( $term_id ) ) {
		return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.'), $term_id );
	}

	$added = add_metadata( 'term', $term_id, $meta_key, $meta_value, $unique );

	// Bust term query cache.
	if ( $added ) {
		wp_cache_set( 'last_changed', microtime(), 'terms' );
	}

	return $added;
}

更新日志

Version 描述
4.4.0 Introduced.

相关函数

Uses

  • wp-includes/taxonomy.php:
    wp_term_is_shared()
  • wp-includes/cache.php:
    wp_cache_set()
  • wp-includes/l10n.php:
    __()
  • wp-includes/option.php:
    get_option()
  • wp-includes/meta.php:
    add_metadata()
  • wp-includes/class-wp-error.php:
    WP_Error::__construct()
  • Show 1 more use
    Hide more uses


User Contributed Notes

  1. Skip to note content

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

    Contributed by Abiral Neupane

    Usage

    add_term_meta( $term_id, $meta_key, $meta_value, $unique );

    Example
    Adds a new custom field with a key name ‘my_term_key’ and value as ‘new_term’

    add_term_meta( 12, "my_term_key" , 'new_term' );

上一篇:add_submenu_page()

下一篇:add_theme_page()

发布评论

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

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