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.
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;
}
Skip to note 2 content
Codex
Example
$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"