重写子主题中的父主题文件夹

时间:2017-01-07 作者:Naz

我有一个父主题,它引用主题的img 像这样的文件夹;

<button id="buy" type="submit"><img src="<?php bloginfo(\'template_directory\'); ?>/img/Buy-Now-Button.jpg" border="0" width="160" height="47" title="" alt="Buy One" style="cursor:pointer;"></button>
然而,我想如果我激活了一个儿童主题,那么在这个儿童主题中还有另一个img具有不同“立即购买”按钮的文件夹。然后使用jpg,而不是父对象中的jpg。

我的目标是允许用户使用他们自己的buy now按钮,如果他们只想将其放在子主题img文件夹中,我这样做对吗?

2 个回复
最合适的回答,由SO网友:Felix 整理而成

尝试使用get_stylesheet_directory_uri() 而不是bloginfo(\'template_directory\').

此函数将为您提供style.css 当前激活的主题(子主题)。从那里,您可以导航到子主题文件夹中的所有子文件夹。

<button id="buy" type="submit"><img src="<?php get_stylesheet_directory_uri(); ?>/img/Buy-Now-Button.jpg" border="0" width="160" height="47" title="" alt="Buy One" style="cursor:pointer;"></button>

SO网友:prosti

板上有一些新功能,最后是一些直观的名称。get_parent_theme_file_uriget_theme_file_uri.

你可以用它代替get_stylesheet_directory_uri().

File: wp-includes/link-template.php
4026: /**
4027:  * Retrieves the URL of a file in the theme.
4028:  *
4029:  * Searches in the stylesheet directory before the template directory so themes
4030:  * which inherit from a parent theme can just override one file.
4031:  *
4032:  * @since 4.7.0
4033:  *
4034:  * @param string $file Optional. File to search for in the stylesheet directory.
4035:  * @return string The URL of the file.
4036:  */
4037: function get_theme_file_uri( $file = \'\' ) {
4038:   $file = ltrim( $file, \'/\' );
4039: 
4040:   if ( empty( $file ) ) {
4041:       $url = get_stylesheet_directory_uri();
4042:   } elseif ( file_exists( get_stylesheet_directory() . \'/\' . $file ) ) {
4043:       $url = get_stylesheet_directory_uri() . \'/\' . $file;
4044:   } else {
4045:       $url = get_template_directory_uri() . \'/\' . $file;
4046:   }
4047: 
4048:   /**
4049:    * Filters the URL to a file in the theme.
4050:    *
4051:    * @since 4.7.0
4052:    *
4053:    * @param string $url  The file URL.
4054:    * @param string $file The requested file to search for.
4055:    */
4056:   return apply_filters( \'theme_file_uri\', $url, $file );
4057: }
以及

File: wp-includes/link-template.php
4059: /**
4060:  * Retrieves the URL of a file in the parent theme.
4061:  *
4062:  * @since 4.7.0
4063:  *
4064:  * @param string $file Optional. File to return the URL for in the template directory.
4065:  * @return string The URL of the file.
4066:  */
4067: function get_parent_theme_file_uri( $file = \'\' ) {
4068:   $file = ltrim( $file, \'/\' );
4069: 
4070:   if ( empty( $file ) ) {
4071:       $url = get_template_directory_uri();
4072:   } else {
4073:       $url = get_template_directory_uri() . \'/\' . $file;
4074:   }
4075: 
4076:   /**
4077:    * Filters the URL to a file in the parent theme.
4078:    *
4079:    * @since 4.7.0
4080:    *
4081:    * @param string $url  The file URL.
4082:    * @param string $file The requested file to search for.
4083:    */
4084:   return apply_filters( \'parent_theme_file_uri\', $url, $file );
4085: }
217个用于在访问资源时安装它们。ZBe.:

File: wp-content/themes/twentyseventeen/inc/custom-header.php
36:     add_theme_support( \'custom-header\', apply_filters( \'twentyseventeen_custom_header_args\', array(
37:         \'default-image\'      => get_parent_theme_file_uri( \'/assets/images/header.jpg\' ),
38:         \'width\'              => 2000,
39:         \'height\'             => 1200,
40:         \'flex-height\'        => true,
41:         \'video\'              => true,
42:         \'wp-head-callback\'   => \'twentyseventeen_header_style\',
43:     ) ) );

相关推荐

Child-theme breaks site

所以,我有一个子主题,里面除了所需的CSS文件之外什么都没有。一旦我激活了这个儿童主题,我的整个网站就关闭了。最后我有两个问题:激活一个只有CSS的子主题怎么能破坏我的网站</我怎样才能回到我原来的主题</这些是网站给我的错误:Warning: require_once(/wp-content/themes/interio_child/admin/options-framework.php) [function.require-once]: 无法打开流:中没有此类文件或目录/wp-c