改进此链接颜色选择器代码

时间:2014-12-19 作者:Andy

我想在管理员中为锚创建一个颜色选择器。我知道这可以通过CSS完成,但我有一个多站点,希望用户可以轻松设置链接颜色。

以下方法可行,但与背景色的颜色选择器不同,颜色显示存在延迟,因此我只是检查以下方面是否可以改进:

function tcx_register_theme_customizer( $wp_customize ) {
    $wp_customize->add_setting(
    \'tcx_link_color\',
    array(
       // \'default\'     => \'#ff0000\'
    )
    );
    $wp_customize->add_control(
    new WP_Customize_Color_Control(
        $wp_customize,
        \'link_color\',
        array(
            \'label\'      => __( \'Link Color\', \'tcx\' ),
            \'section\'    => \'colors\',
            \'settings\'   => \'tcx_link_color\' 
        )
    )
    );
    }
    add_action( \'customize_register\', \'tcx_register_theme_customizer\' );

    function tcx_customizer_css() {
    ?>
    <style type="text/css">
        a { color: <?php echo get_theme_mod( \'tcx_link_color\' ); ?>; }
    </style>
    <?php
    }
    add_action( \'wp_head\', \'tcx_customizer_css\' );
感谢您的帮助

1 个回复
SO网友:Amit Mishra

创建自定义项。js文件,并将以下脚本放入自定义程序中。js文件

(function( $ ) {
    "use strict";   
    wp.customize( \'tcx_link_color\', function( value ) {
        value.bind( function( to ) {
            $( \'a\' ).css( \'color\', to );
        } );
    }); 
})( jQuery );
并将自定义程序排队。在自定义程序中使用wp\\u enqueue\\u脚本创建js文件。像这样的php文件

function tcx_customizer_script() {
    wp_enqueue_script(
        \'tcx-customizer\',
        get_template_directory_uri() . \'/js/customizer.js\',
        array( \'jquery\', \'customize-preview\' ),
        \'0.3.0\',
        true
    );
}
add_action( \'customize_preview_init\', \'tcx_customizer_script\' );
那就好了

结束

相关推荐

Admin_head-post.php仅在发布/更新后才起作用

我用一些管理后端创建了一个自定义帖子类型。目前我正在使用钩子调用脚本admin_head-post.php 但这似乎只有在自定义帖子类型创建(发布时)或更新后才会触发。在特定的管理页面上运行的更好的钩子是什么,但在最初创建新帖子以及更新/发布等时又是什么?