简体   繁体   English

如何在WordPress中获取子主题网址?

[英]How to get child theme url in WordPress?

I am creating a child theme in WordPress. 我正在WordPress中创建子主题。 I have uploaded the assets folder that contains css and javascripts. 我已经上传了包含css和javascripts的资产文件夹。 It will be a custom theme. 这将是一个自定义主题。

Inside the tag i have included the the css file to get the css file. 在标签内部,我包括了css文件以获得css文件。

There is a problem in my current code below: 我下面的当前代码有问题:

<link href="<?php echo get_stylesheet_directory_uri(); ?>/assets/css/icons/icomoon/styles.css" rel="stylesheet" type="text/css">

The code below will work if it is without the styles.css after icomoon folder. 如果下面的代码在icomoon文件夹后没有styles.css,则可以使用。

<link href="<?php echo get_stylesheet_directory_uri(); ?>/assets/css/icons/icomoon" rel="stylesheet" type="text/css">

I want the it to output as: //child_theme_url/assets/css/icons/icomoon/styles.css 我希望它输出为://child_theme_url/assets/css/icons/icomoon/styles.css

I want the styles.css at the end of the include file. 我要在包含文件的末尾添加styles.css。

Please help. 请帮忙。

Using this hook function in your child theme function.php 在您的子主题function.php使用此挂钩function.php

add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
    wp_enqueue_style( 'style', get_template_directory_uri() . '/assets/css/icons/icomoon/styles.css' );

}

Note: get_template_directory_uri() instead of get_stylesheet_directory_uri() 注意:用get_template_directory_uri()代替get_stylesheet_directory_uri()

get_stylesheet_directory_uri() will return the child theme directory url, you got that bit just right. get_stylesheet_directory_uri() 返回子主题目录的url,您恰到好处。 ( get_template_directory_uri() would return the parent theme directory url.) get_template_directory_uri()将返回父主题目录URL。)

Also, if you want styles.css outputted, the first line of code will do that. 另外,如果要输出styles.css ,则第一行代码将执行此操作。 Can't see what could be wrong with it. 无法看到它可能有什么问题。 That means something else is likely not working as intended. 这意味着其他事情可能无法按预期工作。 Are you sure it shouldn't be style.css , for example? 例如,您确定它不应该是style.css吗? Anyway, such an issue would have nothing to do with WordPress correctly returning the child theme url or not. 无论如何,这样的问题与WordPress是否正确返回子主题URL无关。

Well, 好,

to put at the end theme-child/style.css, you write the function (in functions.php): 将theme-child / style.css作为结尾,您编写了函数(在functions.php中):

function sp_enqueue_stylesheets() {
    wp_register_style( 'style-sp', get_stylesheet_directory_uri() . '/style.css', array(), '1.0', 'all' );
    wp_enqueue_style( 'style-sp' );
}
add_action('wp_enqueue_scripts', 'sp_enqueue_stylesheets', 9999 );

Thi is the lastest CSS in the header ;-) Thi是标题中最新的CSS ;-)

对子主题目录使用以下代码。

<?php bloginfo('stylesheet_directory'); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM