个性化阅读
专注于IT技术分析

custom.css.php文件中的WordPress get_theme_mod无法正常工作

点击下载

在Wordpress Theme开发中, 我将一个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文件中的所有链接颜色, 而不是动态打印出头部样式。

谁能帮我了解这里出了什么问题?

这在我的colors.php文件中:

header("content-type: text/css; charset: UTF-8");

$color = get_theme_mod('primary_color', '#1ABC9C'); 

a { color: <?php echo $color; ?>; }

#1


在wp-includes / theme.php中可以找到函数get_theme_mods(以及所有其他与样式相关的函数)

当你创建自定义文件但仍需要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()等功能。

赞(0)
未经允许不得转载:srcmini » custom.css.php文件中的WordPress get_theme_mod无法正常工作

评论 抢沙发

评论前必须登录!