custom.css.php 文件中的 Wordpress get_theme_mod 无法正常工作
Wordpress get_theme_mod in custom.css.php file not working
在 Wordpress 主题开发中,我正在排队一个 colors.php 文件作为样式表。
wp_register_style( 'custom_colors', $uri . '/assets/css/colors.php', [], $ver );
wp_enqueue_style( 'custom_colors' );
我创建了一个 wordpress 定制器部分和设置来管理颜色。我在定制器中添加了我的设置,如下所示:
$wp_customize->add_setting( 'primary_color', [
'default' => '#1ABC9C'
]);
$wp_customize->add_control(
new WP_Customize_Color_Control(
$wp_customize,
'primary_color_input',
array(
'label' => __( 'Primary Accent Color', 'myslug' ),
'section' => 'color_section',
'settings' => 'primary_color',
)
)
);
当我直接在 header.php 文件中调用 get_theme_mod 作为测试以回显它起作用的值时:
$color = get_theme_mod('primary_color', '#1ABC9C'); //hex color is default
echo $color;
但是当我在 colors.php 文件中调用同一行时,出现错误:
Uncaught Error: Call to undefined function get_theme_mods() in /app/public/wp-content/themes/mytheme/assets/css/colors.php:28
我想使用 get_them_mod 值来更新我的 colors.php 文件中的所有 link 颜色,而不是动态打印头部样式。
任何人都可以帮助我了解这里出了什么问题吗?
这在我的 colors.php 文件中:
header("content-type: text/css; charset: UTF-8");
$color = get_theme_mod('primary_color', '#1ABC9C');
a { color: <?php echo $color; ?>; }
函数 get_theme_mods
(以及所有其他与样式相关的函数)位于 wp-includes/theme.php
中
当您正在创建自定义文件,但仍需要wordpress 功能时,您应该告诉wordpress 先加载。这是由 require_once("../howevermanytimes../../wp-load.php")
完成的
之后,您可以测试您需要的函数或该文件中的任何函数是否存在。在这个例子中,它是通过调用
if ( ! function_exists( 'get_theme_mod' ) ) {
require_once( ABSPATH . '/wp-includes/theme.php.' );
}
这可以确保函数已加载。
所有其他函数文件也可以这样做,
所以另一个例子可能是:
if ( ! function_exists( 'get_post_meta' ) ) {
require_once( ABSPATH . '/wp-admin/includes/post.php' );
}
这将使您可以访问 post_exists()
等功能
在 Wordpress 主题开发中,我正在排队一个 colors.php 文件作为样式表。
wp_register_style( 'custom_colors', $uri . '/assets/css/colors.php', [], $ver );
wp_enqueue_style( 'custom_colors' );
我创建了一个 wordpress 定制器部分和设置来管理颜色。我在定制器中添加了我的设置,如下所示:
$wp_customize->add_setting( 'primary_color', [
'default' => '#1ABC9C'
]);
$wp_customize->add_control(
new WP_Customize_Color_Control(
$wp_customize,
'primary_color_input',
array(
'label' => __( 'Primary Accent Color', 'myslug' ),
'section' => 'color_section',
'settings' => 'primary_color',
)
)
);
当我直接在 header.php 文件中调用 get_theme_mod 作为测试以回显它起作用的值时:
$color = get_theme_mod('primary_color', '#1ABC9C'); //hex color is default
echo $color;
但是当我在 colors.php 文件中调用同一行时,出现错误:
Uncaught Error: Call to undefined function get_theme_mods() in /app/public/wp-content/themes/mytheme/assets/css/colors.php:28
我想使用 get_them_mod 值来更新我的 colors.php 文件中的所有 link 颜色,而不是动态打印头部样式。
任何人都可以帮助我了解这里出了什么问题吗?
这在我的 colors.php 文件中:
header("content-type: text/css; charset: UTF-8");
$color = get_theme_mod('primary_color', '#1ABC9C');
a { color: <?php echo $color; ?>; }
函数 get_theme_mods
(以及所有其他与样式相关的函数)位于 wp-includes/theme.php
当您正在创建自定义文件,但仍需要wordpress 功能时,您应该告诉wordpress 先加载。这是由 require_once("../howevermanytimes../../wp-load.php")
之后,您可以测试您需要的函数或该文件中的任何函数是否存在。在这个例子中,它是通过调用
if ( ! function_exists( 'get_theme_mod' ) ) {
require_once( ABSPATH . '/wp-includes/theme.php.' );
}
这可以确保函数已加载。
所有其他函数文件也可以这样做,
所以另一个例子可能是:
if ( ! function_exists( 'get_post_meta' ) ) {
require_once( ABSPATH . '/wp-admin/includes/post.php' );
}
这将使您可以访问 post_exists()
等功能