wp_set_post_terms()
wp_set_post_terms( int $post_id, string|array $tags = ”…
wp_set_post_terms( int $post_id, string|array $tags = ”, string $taxonomy = ‘post_tag’, bool $append = false )
为一篇文章设定条件。
Set the terms for a post.
目录锚点:#说明#参数#源码#笔记
说明(Description)
另请参阅wp_set_object_terms()
参数(Parameters)
| 参数 | 类型 | 说明 |
|---|---|---|
| $post_id | (int) | Post ID.不默认为全局$Post的ID。 |
| $tags | (string | array) | 为post设置的术语数组,或用逗号分隔的一系列术语。层次分类法必须始终传递ID而不是名称,这样名称相同但父项不同的子项不会混淆。 |
| $taxonomy | (string) | 分类名称。 |
| $append | (bool) | 如果为真,不要删除现有的条款,只需添加。如果为false,请用新的术语替换这些术语。 |
源码(Source)
/**
* Set the terms for a post.
*
* @since 2.8.0
*
* @see wp_set_object_terms()
*
* @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post.
* @param string $tags Optional. The tags to set for the post, separated by commas. Default empty.
* @param string $taxonomy Optional. Taxonomy name. Default 'post_tag'.
* @param bool $append Optional. If true, don't delete existing tags, just add on. If false,
* replace the tags with the new tags. Default false.
* @return array|false|WP_Error Array of affected term IDs. WP_Error or false on failure.
*/
function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false ) {
$post_id = (int) $post_id;
if ( !$post_id )
return false;
if ( empty($tags) )
$tags = array();
if ( ! is_array( $tags ) ) {
$comma = _x( ',', 'tag delimiter' );
if ( ',' !== $comma )
$tags = str_replace( $comma, ',', $tags );
$tags = explode( ',', trim( $tags, "
x0B," ) );
}
/*
* Hierarchical taxonomies must always pass IDs rather than names so that
* children with the same names but different parents aren't confused.
*/
if ( is_taxonomy_hierarchical( $taxonomy ) ) {
$tags = array_unique( array_map( 'intval', $tags ) );
}
return wp_set_object_terms( $post_id, $tags, $taxonomy, $append );
}| 更新版本 | 源码位置 | 使用 | 被使用 |
|---|---|---|---|
| 2.8.0 | wp-includes/post.php | 14 | 2 |
笔记(Notes)
非层级术语示例
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
还没有任何评论,赶紧来占个楼吧!