函数文档

wp_set_link_cats()

💡 云策文档标注

概述

wp_set_link_cats() 函数用于更新链接的分类,通过设置链接与指定分类的关联。它处理分类ID数组,确保数据格式正确并调用相关函数更新数据库和缓存。

关键要点

  • 函数接受两个参数:$link_id(必需,链接ID)和 $link_categories(可选,分类ID数组,默认为空数组)。
  • 如果 $link_categories 不是数组或为空,函数会使用默认链接分类ID。
  • 函数内部对分类ID进行整数转换和去重处理,然后调用 wp_set_object_terms() 设置分类关系。
  • 更新后调用 clean_bookmark_cache() 清理链接缓存,确保数据一致性。
  • 该函数自 WordPress 2.1.0 版本引入,常用于 wp_insert_link() 等操作中。

代码示例

wp_set_link_cats( $link_id = 0, $link_categories = array() ) {
    // If $link_categories isn't already an array, make it one:
    if ( ! is_array( $link_categories ) || 0 === count( $link_categories ) ) {
        $link_categories = array( get_option( 'default_link_category' ) );
    }

    $link_categories = array_map( 'intval', $link_categories );
    $link_categories = array_unique( $link_categories );

    wp_set_object_terms( $link_id, $link_categories, 'link_category' );

    clean_bookmark_cache( $link_id );
}

📄 原文内容

Updates link with the specified link categories.

Parameters

$link_idintrequired
ID of the link to update.
$link_categoriesint[]optional
Array of link category IDs to add the link to.

Default:array()

Source

function wp_set_link_cats( $link_id = 0, $link_categories = array() ) {
	// If $link_categories isn't already an array, make it one:
	if ( ! is_array( $link_categories ) || 0 === count( $link_categories ) ) {
		$link_categories = array( get_option( 'default_link_category' ) );
	}

	$link_categories = array_map( 'intval', $link_categories );
	$link_categories = array_unique( $link_categories );

	wp_set_object_terms( $link_id, $link_categories, 'link_category' );

	clean_bookmark_cache( $link_id );
}

Changelog

Version Description
2.1.0 Introduced.