函数文档

_make_cat_compat()

💡 云策文档标注

概述

_make_cat_compat() 函数用于将新的分类法结构更新为旧版(WordPress 2.3 之前)的类别结构,以保持与依赖旧键名或属性名的插件和主题的兼容性。它通过引用传递参数,直接修改传入的变量,无返回值。

关键要点

  • 函数作用:将 WP_Term 对象或数组中的新属性(如 term_id、count)映射到旧属性(如 cat_ID、category_count),以支持向后兼容。
  • 参数要求:必须传递变量(不能内联创建数组或对象),因为参数通过引用传递,否则 PHP 会报错。
  • 返回值:无,所有更新直接在传入的变量上进行。
  • 支持类型:处理对象(包括 WP_Term)和数组,自动检测并更新相应属性。

代码示例

function _make_cat_compat( &$category ) {
    if ( is_object( $category ) && ! is_wp_error( $category ) ) {
        $category->cat_ID               = $category->term_id;
        $category->category_count       = $category->count;
        $category->category_description = $category->description;
        $category->cat_name             = $category->name;
        $category->category_nicename    = $category->slug;
        $category->category_parent      = $category->parent;
    } elseif ( is_array( $category ) && isset( $category['term_id'] ) ) {
        $category['cat_ID']               = &$category['term_id'];
        $category['category_count']       = &$category['count'];
        $category['category_description'] = &$category['description'];
        $category['cat_name']             = &$category['name'];
        $category['category_nicename']    = &$category['slug'];
        $category['category_parent']      = &$category['parent'];
    }
}

注意事项

  • 确保传入的变量是有效的类别对象或数组,否则函数可能不执行任何操作。
  • 在 WordPress 4.4.0 版本后,参数开始支持 WP_Term 对象,增强了类型兼容性。
  • 此函数主要用于内部兼容性处理,开发者通常无需直接调用,但了解其机制有助于调试依赖旧结构的代码。

📄 原文内容

Updates category structure to old pre-2.3 from new taxonomy structure.

Description

This function was added for the taxonomy support to update the new category structure with the old category one. This will maintain compatibility with plugins and themes which depend on the old key or property names.

The parameter should only be passed a variable and not create the array or object inline to the parameter. The reason for this is that parameter is passed by reference and PHP will fail unless it has the variable.

There is no return value, because everything is updated on the variable you pass to it. This is one of the features with using pass by reference in PHP.

Parameters

$categoryarray|object|WP_Termrequired
Category row object or array.

Source

function _make_cat_compat( &$category ) {
	if ( is_object( $category ) && ! is_wp_error( $category ) ) {
		$category->cat_ID               = $category->term_id;
		$category->category_count       = $category->count;
		$category->category_description = $category->description;
		$category->cat_name             = $category->name;
		$category->category_nicename    = $category->slug;
		$category->category_parent      = $category->parent;
	} elseif ( is_array( $category ) && isset( $category['term_id'] ) ) {
		$category['cat_ID']               = &$category['term_id'];
		$category['category_count']       = &$category['count'];
		$category['category_description'] = &$category['description'];
		$category['cat_name']             = &$category['name'];
		$category['category_nicename']    = &$category['slug'];
		$category['category_parent']      = &$category['parent'];
	}
}

Changelog

Version Description
4.4.0 The $category parameter now also accepts a WP_Term object.
2.3.0 Introduced.