我正在构建一个自定义Wordpress主题,并试图通过管理面板添加使用自定义背景的功能。我使用了WP编码中给出的例子。我正在使用WordPress 3.5.2
我得到了背景选项,在我实际查看页面之前,一切似乎都很好。我注意到它添加了一个内部样式,它引用了一个类名为"custom-background"的主体,但主体的实际类是"custom-support"。当我使用Chrome的调试来调整这些时,它会应用正确的样式,所以这是Wordpress函数中的错误吗?我试着找到它能给尸体上那种课的地方,但什么也找不到。
functions.php来自主题
<?php
/*
* Adds the custom header option to the theme
*/
function addthemeoptions(){
//Default values of the header image
$header_defaults = array(
'default-image' => '%s/images/header.png',
'random-default' => false,
'flex-height' => false,
'flex-width' => false,
'default-text-color' => '',
'header-text' => false,
'uploads' => true,
'wp-head-callback' => '',
'admin-head-callback' => '',
'admin-preview-callback' => '',
);
//Adds the support to use custom header images
add_theme_support( 'custom-header', $header_defaults );
$background_defaults = array(
'default-color' => '#000000',
'default-image' => '',
'wp-head-callback' => '_custom_background_cb',
'admin-head-callback' => '',
'admin-preview-callback' => ''
);
add_theme_support( 'custom-background', $background_defaults );
}
//Execute our custom theme functionality
add_action( 'after_setup_theme', 'addthemeoptions' );
?>
头部生成样式
<style type="text/css" id="custom-background-css">
body.custom-background { background-color: #0a0a0a; }
</style>
来自调试的正文标签
<body class=" customize-support" style>
提前感谢
编辑:我找到了一个临时的解决方案,只需在打开body标记的header.php中添加正确的类值,但我觉得应该有一个更完整的解决方案。因为我很难纠正WordPress中的函数应该正确生成的东西?
header.php中的body标记应该是:<body <?php body_class(''); ?>>
我遇到了同样的问题,我在codex自定义后台的最后一行中发现:**要覆盖此默认行为,您必须提供_custom_background_cb()函数的替换项。**
所以我试图通过编码我自己的函数my_custom_background_cb来克服它,如下所示:
$args_background = array(
'default-image' => '',
'default-preset' => 'default', // 'default', 'fill', 'fit', 'repeat', 'custom'
'default-position-x' => 'center', // 'left', 'center', 'right'
'default-position-y' => 'center', // 'top', 'center', 'bottom'
'default-size' => 'auto', // 'auto', 'contain', 'cover'
'default-repeat' => 'repeat', // 'repeat-x', 'repeat-y', 'repeat', 'no-repeat'
'default-attachment' => 'fixed', // 'scroll', 'fixed'
'default-color' => '#fff',
'wp-head-callback' => 'my_custom_background_cb',
'admin-head-callback' => '',
'admin-preview-callback' => '',
);
add_theme_support( 'custom-background', $args_background);
function my_custom_background_cb() {
echo '<style>';
//your custom css
echo '</style>';
}
我现在正在研究如何从默认颜色部分删除背景颜色控件。
编辑:好吧,在customizer.php中,只需删除默认颜色部分,然后像这样添加您的颜色:
// removing the default color sections from customizer
$wp_customize->remove_section('colors');
// colors Options Section
$wp_customize->add_section('sec_colors', array(
'title' => esc_attr__('Colors Options', 'yourtheme'),
'description' => sprintf(__('Colors Options for theme', 'yourtheme')),
'priority' => 60,
));
然后添加您的自定义颜色设置和控件。它对我有用,也希望你。快乐编码
编辑2:很抱歉第二次编辑:D,但我发现我们需要用我们的自定义函数custom_background_cb替换默认的wp-head回调函数,因为我发现了一个错误,我还删除了所有与颜色相关的代码,因为我们创建了这样的颜色选项:
function custom_custom_background_cb() {
// $background is the saved custom image, or the default image.
$background = set_url_scheme( get_background_image() );
if ( ! $background )
return;
$style ='';
if ( $background ) {
$image = " background-image: url('$background');";
$repeat = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) );
if ( ! in_array( $repeat, array( 'no-repeat', 'repeat-x', 'repeat-y', 'repeat' ) ) )
$repeat = 'repeat';
$repeat = " background-repeat: $repeat;";
$position = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) );
if ( ! in_array( $position, array( 'center', 'right', 'left' ) ) )
$position = 'left';
$position = " background-position: top $position;";
$attachment = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) );
if ( ! in_array( $attachment, array( 'fixed', 'scroll' ) ) )
$attachment = 'scroll';
$attachment = " background-attachment: $attachment;";
$style .= $image . $repeat . $position . $attachment;
}
?>
<style type="text/css" id="custom-background-css">
body.custom-background { <?php echo trim( $style ); ?> }
</style>
<?php
}
再次快乐编码
我遇到了这个问题,只打开了一个<script type="text/javascript>
,缺少</script