Wordpress 样式表子主题 [英] Wordpress stylesheet child theme

查看:37
本文介绍了Wordpress 样式表子主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我的问题是我已经有一个功能性的 Child 主题,其中包含一个 functions.php 和许多其他 filesname.php 以及一个可以正常工作的 style.css,但是当我尝试添加其他样式表时,例如模块.min.css 主题仅在父文件夹中读取它,因此我无法更改网站中的样式表,这很烦人

Actually my problem is that I already had a functional Child theme with a functions.php and many others filesname.php and also a style.css which work properly, but when I try to add an other stylesheet like for instance modules.min.css The theme is only reading it in the parent folder so I can't change stylesheets in my website and this is annoying

一些数据:我要覆盖的父文件夹中的文件:\wp-content\themes\startit\assets\css\modules.min.css

Some data: File in the parent folder I want to override: \wp-content\themes\startit\assets\css\modules.min.css

我想要可读的子文件夹中的文件:\wp-content\themes\startit-child\assets\css\modules.min.css

File in the child folder I want to be readable: \wp-content\themes\startit-child\assets\css\modules.min.css

我还尝试将 modules.min.css 放在我的子主题中,紧挨着 style.css,但我无法覆盖父文件夹 \wp-content\themes\startit-child\modules.min.css

I also tryed to put the modules.min.css right here in my child theme next to the styles.css but I can't override the parent folder \wp-content\themes\startit-child\modules.min.css

这就是我的functions.php:

This is what look like my functions.php :

<?php
function startit_enqueue_styles() {

$parent_style = 'startit-style'; // This is 'twentyfifteen-style' for the Twenty Fifteen theme.

wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );

wp_enqueue_style( 'child-style',
    get_stylesheet_directory_uri() . '/style.css',
    array( $parent_style ),
    wp_get_theme()->get('Version')
);
}
add_action( 'wp_enqueue_scripts', 'startit_enqueue_styles' );

?>

谢谢!

推荐答案

有几种方法可以做到这一点,最简单的一种是:

There are couple of ways to do that and the simplest one is:

  1. 移除/出列父 CSS 文件
  2. 添加/加入新的 CSS 文件

  1. Remove/Dequeue Parent CSS file
  2. Add/Enqueue new CSS file

function PREFIX_remove_scripts() {
   wp_dequeue_style( 'put_modules_file_handler_here' );
   wp_deregister_style( 'put_modules_file_handler_here' );

   // Now register your child CSS here, using wp_enqueue_style
}
add_action( 'wp_enqueue_scripts', 'PREFIX_remove_scripts', 20 );

一些常见的混淆您是否应该将子样式和父样式排入队列?有必要吗?这就是在子主题中排队样式表存在很多混淆的地方.答案取决于父样式如何包含在父主题中.例如,如果父主题使用 get_stylesheet_uri() 将样式排入队列,

Some Common Confusions Should you enqueue child styles as well as parent styles? Is it necessary? This is where a lot of confusion exists regarding enqueueing stylesheets in child themes. The answer depends on how the parent styles are included in the parent theme. For example, if the parent theme is using get_stylesheet_uri() to enqueue styles,

例如在二十六:

wp_enqueue_style('twentysixteen-style', get_stylesheet_uri());

那么您不需要将子样式排入队列.这是因为 get_stylesheet_uri() 返回当前活动主题的 URL,在本例中为子主题.

then you don't need to enqueue the child styles. This is because get_stylesheet_uri() returns the URL of the current active theme, which in this case is the child theme.

这是在处理子主题时返回 URL 的方式

get_stylesheet_uri()           = http://example.com/wp-content/themes/example-child/style.css
get_template_directory_uri()   = http://example.com/wp-content/themes/example-parent
get_stylesheet_directory_uri() = http://example.com/wp-content/themes/example-child

因此,我建议您检查您的父 functions.php 并查看样式是如何排队的,以便您可以正确处理它.

So, I would recommend you to check your parent functions.php and see, how the style is enqueued so you can handle it properly.

这篇关于Wordpress 样式表子主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆