Php WordPress-向customizer添加第二个徽标
我想添加到我的主题不同的标志,将用于粘性标题时,用户滚动页面 我试着这样做: 我编辑下划线主题,因此我将代码添加到customizer.php,整个函数如下所示:Php WordPress-向customizer添加第二个徽标,php,wordpress,underscores-wp,Php,Wordpress,Underscores Wp,我想添加到我的主题不同的标志,将用于粘性标题时,用户滚动页面 我试着这样做: 我编辑下划线主题,因此我将代码添加到customizer.php,整个函数如下所示: function mytheme_customize_register( $wp_customize ) { $wp_customize->get_setting( 'blogname' )->transport = 'postMessage'; $wp_customize->get_
function mytheme_customize_register( $wp_customize ) {
$wp_customize->get_setting( 'blogname' )->transport = 'postMessage';
$wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage';
$wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';
if ( isset( $wp_customize->selective_refresh ) ) {
$wp_customize->selective_refresh->add_partial(
'blogname',
array(
'selector' => '.site-title a',
'render_callback' => 'mytheme_customize_partial_blogname',
)
);
$wp_customize->selective_refresh->add_partial(
'blogdescription',
array(
'selector' => '.site-description',
'render_callback' => 'mytheme_customize_partial_blogdescription',
)
);
}
$wp_customize->add_setting('sticky_header_logo');
$wp_customize->add_control(new WP_Customize_Image_Control($wp_customize, 'sticky_header_logo', array(
'label' => 'Sticky Header Logo',
'section' => 'title_tagline',
'settings' => 'sticky_header_logo',
'priority' => 8
)));
}
add_action( 'customize_register', 'mytheme_customize_register' );
在header.php中,我有:
<div class="site-branding">
<?php the_custom_logo(); ?>
</div>
<div class="site-branding-alternative">
<?php get_theme_mod( 'sticky_header_logo' ) ?>
</div>
现在我们已经一步一步地解决了这个问题,我们知道
customizer.php
中的代码正在工作,因为:
返回您的徽标文件,因此问题不在于数据库或主题modsget_theme_mods
显示徽标文件按预期保存在var\u dump
数组键中sticky\u header\u logo
header.php
中,因此我们已经缩小了寻找编码问题的范围。调试的奇迹:)如果我们看一下您用来显示第二个徽标的代码:
<div class="site-branding-alternative">
<?php get_theme_mod( 'sticky_header_logo' ) ?>
</div>
参考资料:
<div class="site-branding-alternative">
<?php
$sticky_logo_url = get_theme_mod( 'sticky_header_logo' );
if ($sticky_logo_url )
echo '<img src="'.$sticky_logo_url.'" alt = "logo alt test" class="sticky_logo_class">';
?>
</div>
在函数中,可以添加任意数量的字段
add_action('customize_register', 'transparent_logo_customize_register');
function transparent_logo_customize_register($wp_customize){}
您是否已检查新徽标是否已保存到数据库中?还是从数据库中检索它的代码有问题?知道问题发生在哪里是解决问题的第一步,因此有关调试的信息对我们来说很重要:)文件已上载到媒体库和站点标识面板,我看到两个徽标。如果您
var_dump(get_theme_mods())代码>你在那里看到你的第二个徽标了吗?它将代码打印到了网站上。我将把它粘贴成两部分,因为它太长了,不适合评论。数组(7){[0]=>bool(false)[“导航菜单位置”]=>数组(1){[“菜单-1”]=>int(2)}[“自定义css\u post\u id”]=>int(-1)[“标题图像”]=>string(75)”[“标题图像数据”]=>object(stdClass){1138(5){“附件id”]=>int(15)[“缩略图”=>url”[]=>感谢您的解释:)在您的代码中没有不必要的结束}但是如果没有这个,它是可以工作的:)@maja Oops,我在整理代码时忘了删除它,我现在就修复它。很高兴它起到了作用!对于任何开发人员来说,最重要的技能是调试,所以我总是试着解释如何调试这个问题,这样它可以帮助人们知道下次做什么……这可能是一个非常繁琐的过程,但非常重要!)
<div class="site-branding-alternative">
<?php
$sticky_logo_url = get_theme_mod( 'sticky_header_logo' );
if ($sticky_logo_url )
echo '<img src="'.$sticky_logo_url.'" alt = "logo alt test" class="sticky_logo_class">';
?>
</div>
add_action('customize_register', 'transparent_logo_customize_register');
function transparent_logo_customize_register($wp_customize){}
$wp_customize->add_setting('transparent_logo');
$wp_customize->add_control(new WP_Customize_Image_Control($wp_customize, 'transparent_logo', array(
'label' => __('Transparent Logo', 'store-front'),
'section' => 'title_tagline',
'settings' => 'transparent_logo',
'priority' => 4,
)));