如何从子主题加载”custom-header.php”

因此, 我在WordPress中创建了一个子主题, 除了custom-header.php之外, 其他所有功能均应正常工作。我希望在子主题中使用一个, 而不在父主题中使用。我这样做是通过尝试从我的子主题中的functions.php文件中调用它来进行的。我在inc文件夹中有我的子主题的custom-header.php文件, 但是已经在主目录中尝试了其余文件。
我的代码(functions.php)

< ?php /** * Enqueues child theme stylesheet, loading first the parent theme stylesheet. */ function nisarg_custom_enqueue_child_theme_styles() { wp_enqueue_style( 'parent-theme-css', get_template_directory_uri() . '/style.css' ); } add_action( 'wp_enqueue_scripts', 'nisarg_custom_enqueue_child_theme_styles', 11 ); /* Problem Code Below */ require_once( get_template_directory_uri() . '/inc/custom-header.php' );

问题问题是这一行:require_once(get_stylesheet_directory()。’ /inc/custom-header.php’ ); 每当我在我的functions.php文件中放置任何require_once语句时, 整个网站都会空白。
我的问题如何加载子主题的custom-header.php而不是父主题的?
错误记录
[Tue May 23 11:54:12.128948 2017] [:error] [pid 30729] [client 192.168.1.1:53819] PHP Warning:require_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /var/www/1to1/wp/wordpress/wp-content/themes/nisarg-child/functions.php on line 11 [Tue May 23 11:54:12.129019 2017] [:error] [pid 30729] [client 192.168.1.1:53819] PHP Warning:require_once(http://conradbowenrealestate.com/wp-content/themes/nisarg/inc/custom-header.php): failed to open stream: no suitable wrapper could be found in /var/www/1to1/wp/wordpress/wp-content/themes/nisarg-child/functions.php on line 11 [Tue May 23 11:54:12.129039 2017] [:error] [pid 30729] [client 192.168.1.1:53819] PHP Fatal error:require_once(): Failed opening required 'http://conradbowenrealestate.com/wp-content/themes/nisarg/inc/custom-header.php' (include_path='.:/usr/share/php') in /var/www/1to1/wp/wordpress/wp-content/themes/nisarg-child/functions.php on line 11

我的代码(custom-header.php)
< ?php /** * * @package Nisarg *//** * Set up the WordPress core custom header feature. * * @uses nisarg_header_style() * @uses nisarg_admin_header_style() * @uses nisarg_admin_header_image() */ function nisarg_custom_header_setup() { add_theme_support( 'custom-header', apply_filters( 'nisarg_custom_header_args', array( 'default-image'=> '', 'default-text-color'=> 'fff', 'width'=> 1600, 'height'=> 400, 'flex-height'=> true, 'wp-head-callback'=> 'nisarg_header_style', 'admin-head-callback'=> 'nisarg_admin_header_style', 'admin-preview-callback' => 'nisarg_admin_header_image', ) ) ); /* * Default custom headers packaged with the theme. * %s is a placeholder for the theme template directory URI. */ register_default_headers( array( 'mountains' => array( 'url'=> '%s/images/headers/mountains.png', 'thumbnail_url' => '%s/images/headers/mountains_thumbnail.png', 'description'=> _x( 'food', 'header image description', 'nisarg' ) ), 'skyline' => array( 'url'=> '%s/images/headers/skyline.png', 'thumbnail_url' => '%s/images/headers/skyline_thumbnail.png', 'description'=> _x( 'buildings', 'header image description', 'nisarg' ) ), 'sea' => array( 'url'=> '%s/images/headers/sea.png', 'thumbnail_url' => '%s/images/headers/sea_thumbnail.png', 'description'=> _x( 'Wood', 'header image description', 'nisarg' ) ), 'food' => array( 'url'=> '%s/images/headers/food.png', 'thumbnail_url' => '%s/images/headers/food_thumbnail.png', 'description'=> _x( 'food', 'header image description', 'nisarg' ) ), ) ); } add_action( 'after_setup_theme', 'nisarg_custom_header_setup' ); if ( ! function_exists( 'nisarg_header_style' ) ) : /** * Styles the header image and text displayed on the blog * * @see nisarg_custom_header_setup(). */ function nisarg_header_style() { $header_image = get_header_image(); $header_text_color= get_header_textcolor(); // If no custom options for text are set, let's bail. if ( empty( $header_image ) & & $header_text_color == get_theme_support( 'custom-header', 'default-text-color' ) ){ return; }// If we get this far, we have custom styles. ?> < style type="text/css" id="nisarg-header-css"> < ?php if ( ! empty( $header_image ) ) : $header_width = get_custom_header()-> width; $header_height = get_custom_header()-> height; $header_height1 = ($header_height / $header_width * 1600); $header_height2 = ($header_height / $header_width * 768); $header_height3 = ($header_height / $header_width * 360); ?> .site-header { background: url(< ?php header_image(); ?> ) no-repeat scroll top; < ?php if($header_height1 > 575){ ?> background-size: 100% auto; padding-bottom:44% < ?php }else{ ?> background-size: 1600px 575px; height: 575px; < ?php } ?> }@media (min-width: 768px) and (max-width: 1024px){ .site-header { < ?php if($header_height2 > 170){ ?> background-size: 1024px auto; height: < ?php echo $header_height2; ?> px; < ?php }else{ ?> background-size: 1024px 170px; height: 170px; < ?php } ?> } }@media (max-width: 767px) { .site-header { < ?php if($header_height2 > 170){ ?> background-size: 768px auto; height: < ?php echo $header_height2; ?> px; < ?php }else{ ?> background-size: 768px 170px; height: 170px; < ?php } ?> } } @media (max-width: 359px) { .site-header { < ?php if($header_height3 > 80){ ?> background-size: 768px auto; height: < ?php echo $header_height3; ?> px; < ?php }else{ ?> background-size: 768px 80px; height: 80px; < ?php } ?> }} .site-header{ -webkit-box-shadow: 0px 0px 2px 1px rgba(182, 182, 182, 0.3); -moz-box-shadow: 0px 0px 2px 1px rgba(182, 182, 182, 0.3); -o-box-shadow: 0px 0px 2px 1px rgba(182, 182, 182, 0.3); box-shadow: 0px 0px 2px 1px rgba(182, 182, 182, 0.3); } < ?php else: ?> .site-header{ -webkit-box-shadow: 0px 0px 1px 1px rgba(182, 182, 182, 0.3); -moz-box-shadow: 0px 0px 1px 1px rgba(182, 182, 182, 0.3); -o-box-shadow: 0px 0px 1px 1px rgba(182, 182, 182, 0.3); box-shadow: 0px 0px 1px 1px rgba(182, 182, 182, 0.3); } .site-header { height: 300px; } @media (max-width: 767px) { .site-header { height: 200px; } } @media (max-width: 359px) { .site-header { height: 150px; } }< ?php endif; // Has the text been hidden? if ( ! display_header_text() ) :?> .site-title, .site-description { position: absolute; clip: rect(1px 1px 1px 1px); /* IE7 */ clip: rect(1px, 1px, 1px, 1px); } < ?php endif; if ( empty( $header_image ) ) : ?> .site-header .home-link { min-height: 0; } < ?php// If the user has set a custom color for the text, use that.else:?> .site-title, .site-description { color: #< ?php echo esc_attr( $header_text_color ); ?> ; } .site-title::after{ background: #< ?php echo esc_attr( $header_text_color ); ?> ; content:""; } < ?php endif; ?> < /style> < ?php } endif; // nisarg_header_styleif ( ! function_exists( 'nisarg_admin_header_style' ) ) :/** * Styles the header image displayed on the Appearance > Header admin panel. * * @see nisarg_custom_header_setup(). */ function nisarg_admin_header_style() { ?> < ?php if ( 'blank' == get_header_textcolor() || '' == get_header_textcolor() ) $style = ' style="display:none; "'; else $style = ' style="color:#' . get_header_textcolor() . '; "'; ?> < style type="text/css"> .appearance_page_custom-header #headimg{ background: #fff; border: none; }#headimg .site-branding { margin:0; position: absolute; top: 50%; left: 50%; margin-right: -50%; -webkit-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); -o-transform: translate(-50%, -50%); transform: translate(-50%, -50%); text-align: center; }#headimg h1 { /* This is the site title displayed in the preview */ < ?php echo esc_attr($style); ?> text-transform: uppercase; letter-spacing: 10px; }#headimg a{ text-decoration: none; }#headimg #desc{ < ?php echo esc_attr($style); ?> text-decoration: none; padding: 0.2em 0em; letter-spacing: 5px; text-transform: capitalize; } #headimg .home-link .displaying-header-text::after{ background: #< ?php get_header_textcolor() ?> ; content:""; height: 2px; display: block; width: 20%; margin: 5px auto; } < /style> < ?php } endif; // nisarg_admin_header_styleif ( ! function_exists( 'nisarg_admin_header_image' ) ) : /** * Custom header image markup displayed on the Appearance > Header admin panel. * * @see nisarg_custom_header_setup(). */ function nisarg_admin_header_image() { ?> < ?php if ( 'blank' == get_header_textcolor() || '' == get_header_textcolor() ) $style = ' style="color: #fff; "'; else $style = ' style="color:#' . get_header_textcolor() . '; "'; ?> < div id="headimg"> < div class="site-branding"> < div class="home-link"> < h1 class="displaying-header-text"> < a id="name" style="< ?php echo esc_attr( $style ); ?> " onclick="return false; " href="http://www.srcmini.com/< ?php esc_url('#'); ?> " tabindex="-1"> < ?php bloginfo( 'name' ); ?> < /a> < /h1> < h2 id="desc" class="displaying-header-text" style="< ?php echo esc_attr( $style ); ?> "> < ?php bloginfo( 'description' ); ?> < /h2> < /div> < /div> < ?php $header_image = get_header_image(); if ( ! empty( $header_image ) ) : ?> < style type="text/css"> .site-header { background: url(< ?php header_image(); ?> ) no-repeat scroll top; } < /style> < ?php endif; ?> < ?php if(''!= get_header_textcolor() || 'blank' != get_header_textcolor()){ ?> < style type="text/css"> #headimg h1::after{ background: #< ?php get_header_textcolor() ?> ; content:""; height: 2px; display: block; width: 20%; margin: 5px auto; } < /style> < ?php } ?> < /div> < ?php } endif; // nisarg_admin_header_image

此问题中发布的所有代码均来自子主题。
#1你正在尝试要求使用URL而不是路径的文件。 get_template_directory_uri()将URL返回到父主题文件夹。
你需要get_stylesheet_directory()才能获得子主题的路径。
替换为:
require_once( get_template_directory_uri() . '/inc/custom-header.php' );

带有:
require_once( get_stylesheet_directory() . '/inc/custom-header.php' );

#2请按照以下步骤操作, 可能对你有帮助
例如, 你的子主题为{父主题名称} -child
步骤1:你的子主题style.css文件应如下所示
/* Theme Name:{Parent theme name} Child Theme Theme URI:http://www.example.com/ Description:Theme by Site < a href=http://www.srcmini.com/"http://www.example.com//\"> http://childthemegenerator.com/< /a> Author:some XYZ Author URI:http://childthemegenerator.com/ Template:{Parent theme name} Version:1.0.0 Text Domain:{Parent theme name}-child -------------------------------------------------------------- */ /* =Theme customization starts here ------------------------------------------------------- */

步骤2:将父主题” inc / custom-header.php” 覆盖为子主题” inc / custom-header.php”
步骤3:编辑每个函数的子主题” inc / custom-header.php” 文件, 因为这些函数已在父主题” inc / custom-header.php” 中定义。
你的子主题” inc / custom-header.php” 文件中使用的功能相同。这就是你获取网站空白的原因。
因此, 你应该重命名子主题” inc / custom-header.php” 文件的每个功能并保存。我已经在子主题” inc / custom-header.php” 文件中重命名了每个函数。
子主题” inc / custom-header.php” 文件应如下所示
< ?php /** * * @package Nisarg Child Theme *//** * Set up the WordPress core custom header feature. * * @uses nisarg_child_header_style() * @uses nisarg_child_admin_header_style() * @uses nisarg_child_admin_header_image() */ function nisarg_child_custom_header_setup() { add_theme_support( 'custom-header', apply_filters( 'nisarg_child_custom_header_args', array( 'default-image'=> '', 'default-text-color'=> 'fff', 'width'=> 1600, 'height'=> 400, 'flex-height'=> true, 'wp-head-callback'=> 'nisarg_child_header_style', 'admin-head-callback'=> 'nisarg_child_admin_header_style', 'admin-preview-callback' => 'nisarg_child_admin_header_image', ) ) ); /* * Default custom headers packaged with the theme. * %s is a placeholder for the theme template directory URI. */ register_default_headers( array( 'mountains' => array( 'url'=> '%s/images/headers/mountains.png', 'thumbnail_url' => '%s/images/headers/mountains_thumbnail.png', 'description'=> _x( 'food', 'header image description', 'nisarg' ), ), 'skyline' => array( 'url'=> '%s/images/headers/skyline.png', 'thumbnail_url' => '%s/images/headers/skyline_thumbnail.png', 'description'=> _x( 'buildings', 'header image description', 'nisarg' ), ), 'sea' => array( 'url'=> '%s/images/headers/sea.png', 'thumbnail_url' => '%s/images/headers/sea_thumbnail.png', 'description'=> _x( 'Wood', 'header image description', 'nisarg' ), ), 'food' => array( 'url'=> '%s/images/headers/food.png', 'thumbnail_url' => '%s/images/headers/food_thumbnail.png', 'description'=> _x( 'food', 'header image description', 'nisarg' ), ), ) ); } add_action( 'after_setup_theme', 'nisarg_child_custom_header_setup' ); if ( ! function_exists( 'nisarg_child_header_style' ) ) : /** * Styles the header image and text displayed on the blog * * @see nisarg_custom_header_setup(). */ function nisarg_child_header_style() { $header_image = get_header_image(); $header_text_color= get_header_textcolor(); // If no custom options for text are set, let's bail. if ( empty( $header_image ) & & $header_text_color == get_theme_support( 'custom-header', 'default-text-color' ) ) { return; } // If we get this far, we have custom styles. ?> < style type="text/css" id="nisarg-header-css"> < ?php if ( ! empty( $header_image ) ) : $header_width = get_custom_header()-> width; $header_height = get_custom_header()-> height; $header_height320 = ( $header_height / $header_width * 320 ); $header_height360 = ( $header_height / $header_width * 360 ); $header_height768 = ( $header_height / $header_width * 768 ); $header_height980 = ( $header_height / $header_width * 980 ); $header_height1280 = ( $header_height / $header_width * 1280 ); $header_height1366 = ( $header_height / $header_width * 1366 ); $header_height1440 = ( $header_height / $header_width * 1440 ); $header_height1600 = ( $header_height / $header_width * 1600 ); $header_height1920 = ( $header_height / $header_width * 1920 ); $header_height2560 = ( $header_height / $header_width * 2560 ); $header_height2880 = ( $header_height / $header_width * 2880 ); ?> .site-header { background: url(< ?php header_image(); ?> ) no-repeat scroll top; background-size: cover; } @media (min-width: 320px) and (max-width: 359px ) { .site-header { height: < ?php echo absint( $header_height320 ); ?> px; } } @media (min-width: 360px) and (max-width: 767px ) { .site-header { height: < ?php echo absint( $header_height360 ); ?> px; } } @media (min-width: 768px) and (max-width: 979px ) { .site-header { height: < ?php echo absint( $header_height768 ); ?> px; } } @media (min-width: 980px) and (max-width: 1279px ){ .site-header { height: < ?php echo absint( $header_height980 ); ?> px; } } @media (min-width: 1280px) and (max-width: 1365px ){ .site-header { height: < ?php echo absint( $header_height1280 ); ?> px; } } @media (min-width: 1366px) and (max-width: 1439px ){ .site-header { height: < ?php echo absint( $header_height1366 ); ?> px; } } @media (min-width: 1440px) and (max-width: 1599px ) { .site-header { height: < ?php echo absint( $header_height1440 ); ?> px; } } @media (min-width: 1600px) and (max-width: 1919px ) { .site-header { height: < ?php echo absint( $header_height1600 ); ?> px; } } @media (min-width: 1920px) and (max-width: 2559px ) { .site-header { height: < ?php echo absint( $header_height1920 ); ?> px; } } @media (min-width: 2560px)and (max-width: 2879px ) { .site-header { height: < ?php echo absint( $header_height2560 ); ?> px; } } @media (min-width: 2880px) { .site-header { height: < ?php echo absint( $header_height2880 ); ?> px; } } .site-header{ -webkit-box-shadow: 0px 0px 2px 1px rgba(182, 182, 182, 0.3); -moz-box-shadow: 0px 0px 2px 1px rgba(182, 182, 182, 0.3); -o-box-shadow: 0px 0px 2px 1px rgba(182, 182, 182, 0.3); box-shadow: 0px 0px 2px 1px rgba(182, 182, 182, 0.3); } < ?php else : ?> .site-header{ -webkit-box-shadow: 0px 0px 1px 1px rgba(182, 182, 182, 0.3); -moz-box-shadow: 0px 0px 1px 1px rgba(182, 182, 182, 0.3); -o-box-shadow: 0px 0px 1px 1px rgba(182, 182, 182, 0.3); box-shadow: 0px 0px 1px 1px rgba(182, 182, 182, 0.3); } .site-header { height: 300px; } @media (max-width: 767px) { .site-header { height: 200px; } } @media (max-width: 359px) { .site-header { height: 150px; } } < ?php endif; // Has the text been hidden? if ( ! display_header_text() ) : ?> .site-title, .site-description { position: absolute; clip: rect(1px 1px 1px 1px); /* IE7 */ clip: rect(1px, 1px, 1px, 1px); } < ?php endif; if ( empty( $header_image ) ) : ?> .site-header .home-link { min-height: 0; } < ?php // If the user has set a custom color for the text, use that. else : ?> .site-title, .site-description { color: #< ?php echo esc_attr( $header_text_color ); ?> ; } .site-title::after{ background: #< ?php echo esc_attr( $header_text_color ); ?> ; content:""; } < ?php endif; ?> < /style> < ?php } endif; // nisarg_child_header_styleif ( ! function_exists( 'nisarg_child_admin_header_style' ) ) : /** * Styles the header image displayed on the Appearance > Header admin panel. * * @see nisarg_custom_header_setup(). */ function nisarg_child_admin_header_style() { ?> < ?php if ( 'blank' == get_header_textcolor() || '' == get_header_textcolor() ) { $style = ' style="display:none; "'; } else { $style = ' style="color:#' . get_header_textcolor() . '; "'; } ?> < style type="text/css"> .appearance_page_custom-header #headimg{ background: #fff; border: none; }#headimg .site-branding { margin:0; position: absolute; top: 50%; left: 50%; margin-right: -50%; -webkit-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); -o-transform: translate(-50%, -50%); transform: translate(-50%, -50%); text-align: center; } #headimg h1 { /* This is the site title displayed in the preview */ < ?php echo esc_attr( $style ); ?> text-transform: uppercase; letter-spacing: 10px; }#headimg a{ text-decoration: none; } #headimg #desc{ < ?php echo esc_attr( $style ); ?> text-decoration: none; padding: 0.2em 0em; letter-spacing: 5px; text-transform: capitalize; } #headimg .home-link .displaying-header-text::after{ background: #< ?php get_header_textcolor() ?> ; content:""; height: 2px; display: block; width: 20%; margin: 5px auto; } < /style> < ?php } endif; // nisarg_child_admin_header_styleif ( ! function_exists( 'nisarg_child_admin_header_image' ) ) : /** * Custom header image markup displayed on the Appearance > Header admin panel. * * @see nisarg_child_custom_header_setup(). */ function nisarg_child_admin_header_image() { if ( 'blank' == get_header_textcolor() || '' == get_header_textcolor() ) { $style = ' style="color: #fff; "'; } else { $style = ' style="color:#' . get_header_textcolor() . '; "'; } ?> < div id="headimg"> < div class="site-branding"> < div class="home-link"> < h1 class="displaying-header-text"> < a id="name" style="< ?php echo esc_attr( $style ); ?> " onclick="return false; " href="http://www.srcmini.com/< ?php esc_url('#'); ?> " tabindex="-1"> < ?php bloginfo( 'name' ); ?> < /a> < /h1> < h2 id="desc" class="displaying-header-text" style="< ?php echo esc_attr( $style ); ?> "> < ?php bloginfo( 'description' ); ?> < /h2> < /div> < /div> < ?php $header_image = get_header_image(); if ( ! empty( $header_image ) ) : ?> < style type="text/css"> .site-header { background: url(< ?php header_image(); ?> ) no-repeat scroll top; } < /style> < ?php endif; ?> < ?php if ( '' != get_header_textcolor() || 'blank' != get_header_textcolor() ) { ?> < style type="text/css"> #headimg h1::after{ background: #< ?php get_header_textcolor() ?> ; content:""; height: 2px; display: block; width: 20%; margin: 5px auto; } < /style> < ?php } ?> < /div> < ?php } endif; // nisarg_child_admin_header_image

步骤4:你的孩子主题fnctions.php应该像这样
< ?php add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' ); function my_theme_enqueue_styles() { wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' ); } require_once( get_stylesheet_directory() . '/inc/custom-header.php' ); ?>

#3通过使用get_header()函数可以为你提供帮助
【如何从子主题加载” custom-header.php” 】你的自定义标头名称应类似于” header-name.php” , 然后在该函数中调用自定义标头名称。
< ?php get_header('name'); ?>

    推荐阅读