custom.css.php文件中的Wordpress get_theme_mod不工作
在Wordpress主题开发中,我将一个colors.php文件作为样式表进行排队custom.css.php文件中的Wordpress get_theme_mod不工作,php,wordpress,wordpress-theming,stylesheet,Php,Wordpress,Wordpress Theming,Stylesheet,在Wordpress主题开发中,我将一个colors.php文件作为样式表进行排队 wp_register_style( 'custom_colors', $uri . '/assets/css/colors.php', [], $ver ); wp_enqueue_style( 'custom_colors' ); 我已经创建了一个wordpress自定义部分和设置来管理颜色。我在自定义程序中添加了我的设置,如下所示: $wp_customize->add_setting( 'pri
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文件中的所有链接颜色,而不是在头部动态打印样式
有人能帮我理解这里出了什么问题吗
在my colors.php文件中:
header("content-type: text/css; charset: UTF-8");
$color = get_theme_mod('primary_color', '#1ABC9C');
a { color: <?php echo $color; ?>; }
标题(“内容类型:text/css;字符集:UTF-8”);
$color=get_theme_mod('primary_color','1ABC9C');
a{color:;}
函数get\u theme\u mods
(以及所有其他与样式相关的函数)可以在wp includes/theme.php中找到
当您正在创建自定义文件,但仍然需要wordpress函数时,您应该告诉wordpress先加载。这是通过require\u 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()
等函数。您可能需要在css/php文件中首先包含wp includes/theme.php。
像嘿,Stender这样的函数工作得非常好!非常感谢你!你能把它作为答案吗?如果可能的话,你能向我解释一下,包括那些文件在内,你都做了些什么吗?非常好而且信息丰富!谢谢你在这个问题上的帮助,我真的很感激。