【问题标题】:How to get child theme url in WordPress?如何在 WordPress 中获取子主题 url?
【发布时间】:2016-07-13 10:27:45
【问题描述】:

我正在 WordPress 中创建一个子主题。我已经上传了包含 css 和 javascripts 的资产文件夹。这将是一个自定义主题。

在标签内我已经包含了 css 文件以获取 css 文件。

下面我当前的代码有问题:

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

如果 icomoon 文件夹后没有 styles.css,下面的代码将起作用。

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

我希望它输出为: //child_theme_url/assets/css/icons/icomoon/styles.css

我想要包含文件末尾的 styles.css。

请帮忙。

【问题讨论】:

标签: javascript php html css wordpress


【解决方案1】:

在你的子主题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' );

}

注意:get_template_directory_uri() 而不是 get_stylesheet_directory_uri()

【讨论】:

    【解决方案2】:

    get_stylesheet_directory_uri() 返回子主题目录 url,你说得对。 (get_template_directory_uri() 将返回父主题目录 url。)

    另外,如果你想输出styles.css,第一行代码就可以做到。看不出有什么问题。这意味着其他东西可能无法按预期工作。例如,您确定不应该是style.css 吗?无论如何,这样的问题与 WordPress 是否正确返回子主题 URL 无关。

    【讨论】:

      【解决方案3】:

      嗯,

      把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 );
      

      这是标题中最新的 CSS ;-)

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

        • 回显它而不是仅仅返回值。
        猜你喜欢
        • 2013-12-02
        • 1970-01-01
        • 2017-08-20
        • 2017-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-05
        • 1970-01-01
        相关资源
        最近更新 更多