函数文档

get_theme_mods()

💡 云策文档标注

概述

get_theme_mods() 函数用于检索当前主题的所有主题修改设置,返回一个数组。该函数还处理旧版 WordPress 中已弃用的主题修改选项键的迁移。

关键要点

  • 函数返回当前主题的所有主题修改设置数组,若不存在则返回空数组。
  • 自动将旧版 WordPress 中使用的已弃用选项键 mods_$theme_name 迁移到新键 theme_mod_$name,并在管理界面中更新和删除旧选项。
  • 从 WordPress 3.1.0 版本引入,5.9.0 版本起返回值始终为数组。

代码示例

$mods = get_theme_mods();

var_dump( $mods );
// output example:
//    array(2) { ["header_textcolor"]=> string(3) "333" ["header_image"]=> string(20) "random-default-image" }

var_dump( $mods['header_textcolor'] );
// output example:
//    string(3) "333"

📄 原文内容

Retrieves all theme modifications.

Return

array Theme modifications.

More Information

This function will update the options for theme mods which were created in older WordPress versions that used the deprecated mods_$theme_name option key to now use theme_mod_$name.

Source

function get_theme_mods() {
	$theme_slug = get_option( 'stylesheet' );
	$mods       = get_option( "theme_mods_$theme_slug" );

	if ( false === $mods ) {
		$theme_name = get_option( 'current_theme' );
		if ( false === $theme_name ) {
			$theme_name = wp_get_theme()->get( 'Name' );
		}

		$mods = get_option( "mods_$theme_name" ); // Deprecated location.
		if ( is_admin() && false !== $mods ) {
			update_option( "theme_mods_$theme_slug", $mods );
			delete_option( "mods_$theme_name" );
		}
	}

	if ( ! is_array( $mods ) ) {
		$mods = array();
	}

	return $mods;
}

Changelog

Version Description
5.9.0 The return value is always an array.
3.1.0 Introduced.

User Contributed Notes