【问题标题】:How to load style.css after fonts.php in wordpress?如何在 wordpress 中的 fonts.php 之后加载 style.css?
【发布时间】:2015-09-09 18:32:43
【问题描述】:

我正在 WordPress 中本地开发一个网站,然后将其上传到临时开发服务器,以定期向管理层展示我的进度。

问题: 当我将所有内容上传到服务器时,我注意到 style.css 中的样式被 fonts.php 中的样式覆盖。这是我第一次使用 Wordpress 平台。我不知道为什么,但是当我在本地托管时情况并非如此。如图所示,fonts.php 在 style.css 之后声明。 图片:

我尝试了什么: 我正在深入研究 wordpress 文件以找到一种方法来先声明 fonts.php,然后再声明 style.css,以便 style.css 将覆盖 fonts.php。我在 wp-includes/theme.php 中找到了这个文件,我发现 style.css 被声明但找不到 fonts.php。这是一个死胡同。

有人知道怎么做吗?

【问题讨论】:

  • 您好,欢迎来到 stackoverflow。友情提示:提问时请将相关代码粘贴到问题中。不要使用代码编辑器或 html 源代码的屏幕截图。它难以阅读且不可搜索。
  • 很抱歉。谢谢告知,以后不会这样了。

标签: php css wordpress overriding declare


【解决方案1】:
// Load the theme stylesheets
function theme_styles()  
{ 

    // Example of loading a jQuery slideshow plugin just on the homepage
    wp_register_style( 'flexslider', get_template_directory_uri() . '/css/flexslider.css' );

    // Load all of the styles that need to appear on all pages
    wp_enqueue_style( 'main', get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'custom', get_template_directory_uri() . '/css/custom.css' );

    // Conditionally load the FlexSlider CSS on the homepage
    if(is_page('home')) {
        wp_enqueue_style('flexslider');
    }

}
add_action('wp_enqueue_scripts', 'theme_styles');

【讨论】:

  • 正如我在问题中所说的那样,我发现了这一点。如何确保我们在它之前声明了 fonts.php?
【解决方案2】:

让我们举个例子,在 wordpress 中使用 wp_enqueue_style() 导入 Bootstrap CSS 样式表

首先你需要插入

`<?php wp_head(); ?>` 

所以你的主题可以使用过滤器和使用 wp_head() 钩子的钩子。这样任何主题添加都可以轻松调用。

确保你有 wp_head();在你的主题的 head 部分之间,在 header.php 文件中。

然后在index.php也存在的主题目录下新建一个文件functions.php。在functions.php中,我们将创建一个新函数,然后使用 wp_register_script 函数注册样式,然后使用 wp_enqueue_style 函数调用我们的样式。

这是我们将用于在 Bootstrap 样式表中调用的代码。

`<?php
function add_stylesheet() {
wp_register_style('bootstrap.min',get_template_directory_uri() . 
'/css/bootstrap.min.css');
wp_enqueue_style( 'bootstrap.min' );
}
add_action( 'wp_enqueue_scripts', 'add_stylesheet' );
?>`

那么上面的代码是做什么的呢?

add_stylesheet() 是我们创建的函数的名称,你可以随意命名。

然后,我们通过 WordPress 函数 wp_register_style 注册了我们的样式表。它接受5个不同的参数

`$handle, $src, $deps, $ver, and $in_footer`

注册样式完成后,我们可以使用 WordPress 函数 wp_enqueue_style 调用它。就是这样,这个方法一开始可能看起来很复杂,但这是在 WordPress 中调用样式表的首选方法。

【讨论】:

    【解决方案3】:

    感谢 Susheel 的帖子。如果我必须在外部添加一个 css 文件但没有解决我的问题,那就是确保在此之前加载 fonts.php,这会很有帮助。

    我找到了一个简单的解决方案。 我在adaptive.css文件中移动了我想在fonts.php上覆盖的所有css,该文件总是在fonts.php文件之后加载。

    【讨论】:

      猜你喜欢
      • 2021-02-14
      • 2016-08-09
      • 2021-06-13
      • 1970-01-01
      • 2014-08-21
      • 2019-05-27
      • 2020-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多