get_theme_mod

函数
get_theme_mod ( $name, $default = false )
参数
  • (string) $name Theme modification name.
    Required:
  • (mixed) $default Optional. Theme modification default value. Default false.
    Required:
    Default: false
返回值
  • (mixed) Theme modification value.
定义位置
相关方法
get_theme_modsset_theme_modget_themeget_theme_rootget_theme_data
引入
2.1.0
弃用
-

get_theme_mod: 这个函数用来检索一个主题修改设置值。它接受两个参数:修改设置的名称和一个默认值(如果修改设置不存在)。

为活动主题检索主题修改值。

如果修改名称不存在,并且`$default`是一个字符串,那么默认值将通过{@link sprintf()}PHP函数传递,模板目录URI作为第一个值,样式表目录URI作为第二个值。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function get_theme_mod( $name, $default = false ) {
$mods = get_theme_mods();
if ( isset( $mods[ $name ] ) ) {
/**
* Filters the theme modification, or 'theme_mod', value.
*
* The dynamic portion of the hook name, `$name`, refers to the key name
* of the modification array. For example, 'header_textcolor', 'header_image',
* and so on depending on the theme options.
*
* @since 2.2.0
*
* @param mixed $current_mod The value of the active theme modification.
*/
return apply_filters( "theme_mod_{$name}", $mods[ $name ] );
}
if ( is_string( $default ) ) {
// Only run the replacement if an sprintf() string format pattern was found.
if ( preg_match( '#(?<!%)%(?:d+$?)?s#', $default ) ) {
// Remove a single trailing percent sign.
$default = preg_replace( '#(?<!%)%$#', '', $default );
$default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
}
}
/** This filter is documented in wp-includes/theme.php */
return apply_filters( "theme_mod_{$name}", $default );
}
function get_theme_mod( $name, $default = false ) { $mods = get_theme_mods(); if ( isset( $mods[ $name ] ) ) { /** * Filters the theme modification, or 'theme_mod', value. * * The dynamic portion of the hook name, `$name`, refers to the key name * of the modification array. For example, 'header_textcolor', 'header_image', * and so on depending on the theme options. * * @since 2.2.0 * * @param mixed $current_mod The value of the active theme modification. */ return apply_filters( "theme_mod_{$name}", $mods[ $name ] ); } if ( is_string( $default ) ) { // Only run the replacement if an sprintf() string format pattern was found. if ( preg_match( '#(?<!%)%(?:d+$?)?s#', $default ) ) { // Remove a single trailing percent sign. $default = preg_replace( '#(?<!%)%$#', '', $default ); $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() ); } } /** This filter is documented in wp-includes/theme.php */ return apply_filters( "theme_mod_{$name}", $default ); }
function get_theme_mod( $name, $default = false ) {
	$mods = get_theme_mods();

	if ( isset( $mods[ $name ] ) ) {
		/**
		 * Filters the theme modification, or 'theme_mod', value.
		 *
		 * The dynamic portion of the hook name, `$name`, refers to the key name
		 * of the modification array. For example, 'header_textcolor', 'header_image',
		 * and so on depending on the theme options.
		 *
		 * @since 2.2.0
		 *
		 * @param mixed $current_mod The value of the active theme modification.
		 */
		return apply_filters( "theme_mod_{$name}", $mods[ $name ] );
	}

	if ( is_string( $default ) ) {
		// Only run the replacement if an sprintf() string format pattern was found.
		if ( preg_match( '#(?<!%)%(?:d+$?)?s#', $default ) ) {
			// Remove a single trailing percent sign.
			$default = preg_replace( '#(?<!%)%$#', '', $default );
			$default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
		}
	}

	/** This filter is documented in wp-includes/theme.php */
	return apply_filters( "theme_mod_{$name}", $default );
}

常见问题

FAQs
查看更多 >