php - 如何在自定义主题中个性化 Wordpress 颜色?
问题描述
我在网站的某处找到了我需要做的一部分的代码。它添加了 1 个颜色选择器,但我尝试添加多个选择器,但找不到正确的方法。
function mytheme_customize_register( $wp_customize ) {
//All our sections, settings, and controls will be added here
$wp_customize->add_setting( 'header_textcolor' , array(
'default' => "#000000",
'transport' => 'refresh',
) );
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'header_textcolor', array(
'label' => __( 'Header Color', 'mytheme' ),
'section' => 'colors',
) ) );
}
add_action( 'customize_register', 'mytheme_customize_register' );
function mytheme_customize_css()
{
?>
<style type="text/css">
h2 { color: #<?php echo get_theme_mod('header_textcolor', "#000000"); ?>; }
</style>
<?php
}
add_action( 'wp_head', 'mytheme_customize_css');
我试图找到做广告多重选择器的方法,一个用于 h1,一个用于 h2,一个用于链接,...例如。
有人知道如何以正确的方式进行吗?我尝试了几件事,我可以让它出现,但是它们不活跃,我不熟悉 WP 结构:/
这是我想要做的结构:
/** OPTION COLOR LINK**/
function mytheme_customize_register_link( $wp_customize ) {
//All our sections, settings, and controls will be added here
$wp_customize->add_setting( 'header_textcolor' , array(
'default' => "#000000",
'transport' => 'refresh',
) );
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'header_textcolor', array(
'label' => __( 'Link', 'mytheme' ),
'section' => 'colors',
) ) );
}
add_action( 'customize_register', 'mytheme_customize_register_link' );
function mytheme_customize_css_link()
{
?>
<style type="text/css">
a { color: #<?php echo get_theme_mod('header_textcolor', "#000000"); ?>; }
</style>
<?php
}
add_action( 'wp_head', 'mytheme_customize_css_link');
/** OPTION COLOR LINK HOVER**/
function mytheme_customize_register_link_hover( $wp_customize ) {
//All our sections, settings, and controls will be added here
$wp_customize->add_setting( 'header_textcolor' , array(
'default' => "#000000",
'transport' => 'refresh',
) );
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'header_textcolor', array(
'label' => __( 'Link Hover', 'mytheme' ),
'section' => 'colors',
) ) );
}
add_action( 'customize_register', 'mytheme_customize_register_link_hover' );
function mytheme_customize_css_link_hover()
{
?>
<style type="text/css">
a:hover, a:active { color: #<?php echo get_theme_mod('header_textcolor', "#000000"); ?>; }
</style>
<?php
}
add_action( 'wp_head', 'mytheme_customize_css_link_hover');
但我知道这不是这里的东西,但不明白是什么。
解决方案
嗨,我相信我迟到了几个月,可能你已经解决了这个问题,但这里有一个解决方案可以帮助有同样问题的人。
function mytheme_customize_register( $wp_customize ) {
//All our sections, settings, and controls will be added here
$wp_customize->add_setting( 'link-color' , array(
'default' => "#000000",
'transport' => 'refresh',
) );
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'link-color', array(
'label' => __( 'Link Color', 'mytheme' ),
'section' => 'colors',
) ) );
//All our sections, settings, and controls will be added here
$wp_customize->add_setting( 'link-hover' , array(
'default' => "#ffffff",
'transport' => 'refresh',
) );
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'link-hover', array(
'label' => __( 'Link Hover', 'mytheme' ),
'section' => 'colors',
) ) );
}
add_action( 'customize_register', 'mytheme_customize_register' );
function mytheme_customize_css() {
?>
<style type="text/css">
/* link color style define !important if need*/
a { color: <?php echo get_theme_mod('link-color', "#000000"); ?> !important; }
/* link color hover effect style define !important if need*/
a:hover { color: <?php echo get_theme_mod('link-hover', "#ffffff"); ?> !important; }
</style>
<?php
}
add_action( 'wp_head', 'mytheme_customize_css');
我进行了测试,它的工作原理如下所示
推荐阅读
- c# - LinqPad6 无法加载程序集以连接到我的项目
- amazon-web-services - 从 Step Function 错误启动 Codebuild 作业:....“未授权创建托管规则”
- android - androidx房间多个MATCH
- firebase - Flutter 上的 Uri.https 限制了我的 url 的长度
- python - 从python中的字典中提取最大值和最小值
- html - 如何为xhtml中的每个选择创建一个包含单独信息的下拉列表
- python - 烧瓶和保留连接对象
- excel - 如果我按另一列对轴进行排序,DAX Measure 不再准确计算?
- python - IBapi Python - 从 reqHistoricalData 获取最后价格作为变量?
- office365 - 使用 Office 插件插入可点击的图像或对象