函数文档

get_linkcatname()

💡 云策文档标注

概述

get_linkcatname() 是一个已弃用的 WordPress 函数,用于通过链接 ID 获取关联分类的名称。自 2.1.0 版本起,建议使用 get_category() 替代。

关键要点

  • 函数已弃用:自 WordPress 2.1.0 起,应改用 get_category() 函数。
  • 功能:根据链接 ID 获取其第一个关联分类的名称。
  • 参数:$id(整数,可选,默认为 0),指定链接 ID。
  • 返回值:字符串,分类名称;若无分类或输入无效则返回空字符串。
  • 内部调用:依赖 wp_get_link_cats() 获取分类 ID,再通过 get_category() 获取名称。

代码示例

function get_linkcatname($id = 0) {
    _deprecated_function( __FUNCTION__, '2.1.0', 'get_category()' );

    $id = (int) $id;

    if ( empty($id) )
        return '';

    $cats = wp_get_link_cats($id);

    if ( empty($cats) || ! is_array($cats) )
        return '';

    $cat_id = (int) $cats[0]; // Take the first cat.

    $cat = get_category($cat_id);
    return $cat->name;
}

注意事项

  • 此函数已弃用,新代码中应避免使用,改用 get_category() 以保持兼容性。
  • 函数内部使用 _deprecated_function() 标记弃用,调用时会触发相关通知。
  • 参数 $id 为 0 或空时返回空字符串,需确保传入有效链接 ID。

📄 原文内容

Gets the name of category by ID.

Description

See also

Parameters

$idintrequired
The category to get. If no category supplied uses 0

Return

string

Source

function get_linkcatname($id = 0) {
	_deprecated_function( __FUNCTION__, '2.1.0', 'get_category()' );

	$id = (int) $id;

	if ( empty($id) )
		return '';

	$cats = wp_get_link_cats($id);

	if ( empty($cats) || ! is_array($cats) )
		return '';

	$cat_id = (int) $cats[0]; // Take the first cat.

	$cat = get_category($cat_id);
	return $cat->name;
}

Changelog

Version Description
2.1.0 Deprecated. Use get_category()
0.71 Introduced.