【问题标题】:Wordpress @import or enqueue and does this code make sense?Wordpress @import 或 enqueue,这段代码有意义吗?
【发布时间】:2014-10-15 19:31:45
【问题描述】:

刚刚发现@import 不是导入样式表的最有效方法,尝试使用 enqueue。这有意义吗,尝试在不同的文件夹中导入几个父样式表:

<?php
/**
 * Load the style sheet from the parent theme.
 *
 */
function theme_name_parent_styles() {

  // Enqueue the parent stylesheet
  wp_enqueue_style( 'theme-name-parent-style', get_template_directory_uri() . '/style.css', array(), '0.1', 'all' );
  wp_enqueue_style( 'theme-name-parent-style', get_template_directory_uri() . '/css/custom-admin-style.css', array(), '1', 'all' );
  wp_enqueue_style( 'theme-name-parent-style', get_template_directory_uri() . '/css/flexslider.css', array(), '1', 'all' );
  wp_enqueue_style( 'theme-name-parent-style', get_template_directory_uri() . '/css/thumbfx.css', array(), '1', 'all' );
  wp_enqueue_style( 'theme-name-parent-style', get_template_directory_uri() . '/css/dynamic-css/options.css', array(), '1', 'all' );


  // Enqueue the parent rtl stylesheet
  if ( is_rtl() ) {
    wp_enqueue_style( 'theme-name-parent-style-rtl', get_template_directory_uri() . '/rtl.css', array(), '0.1', 'all' );
  }

}
add_action( 'wp_enqueue_scripts', 'theme_name_parent_styles' );
?>

【问题讨论】:

  • 您能否提供有关您的问题的更多信息,或者这是一个代码审查请求?关于代码 - 您应该为不同的脚本使用不同的处理程序(入队函数的第一个参数),否则代码看起来没问题,这是在站点头部包含脚本的正确方法。
  • 在我的functions.php子主题中添加了这个队列,因为这样做我的主要字体改变了试图让它回来。这是一个谜……考虑到所有样式表都已加载。
  • 您是否使用 font-face 作为您的字体,如果是,在哪个 css 文件中?另外,您是否在某处使用 css 导入,例如:@import url('/css/fonts.css')?
  • 我在 Wordpress codex 中阅读时删除了@import,最好在子主题中的 style.css 上入队而不是导入...所以我将 function.php 添加到带有入队的子项中。
  • 您的问题可能出在 get_template_directory_uri() 函数中 - 它在子主题中使用时返回父主题的路径。检查get_template_directory_uriget_stylesheet_directory_uri

标签: css wordpress stylesheet


【解决方案1】:

第一条规则:遵循wordpress codex

<?php

add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_styles', PHP_INT_MAX);
function enqueue_child_theme_styles() {
    wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
    wp_enqueue_style( 'child-style', get_stylesheet_uri(), array('parent-style')  );
}

下次我会先检查source

已解决,感谢@m1ro 的疑难解答。

【讨论】:

  • 我很高兴您发现我最近对抄本的补充很有用。随时查看我在此主题上所做的帖子here。 +1 用于添加您的解决方案。请记住在限制到期时接受您自己的答案
猜你喜欢
  • 1970-01-01
  • 2021-09-05
  • 2015-03-01
  • 2011-06-10
  • 2015-06-01
  • 2018-12-06
  • 1970-01-01
  • 1970-01-01
  • 2010-10-31
相关资源
最近更新 更多