【问题标题】:Wordpress not working after adding style.css添加 style.css 后 Wordpress 无法正常工作
【发布时间】:2016-05-04 10:20:54
【问题描述】:

一段时间以来,我一直在努力寻找解决方案,但没有运气... 我正忙于根据this article 为 WordPress 制作自定义主题。 get_header() 之类的 html 和 php 钩子运行良好,但是一旦我尝试添加 style.css 它就不起作用并且“演示主题”被卡住了在加载时。但是,当我删除它时,它工作得很好,但是 style.css 文件中的 css 没有加载。 这些是我想加载到页面中的 css 文件。

<link rel="stylesheet" href="http://www.website.com/wp-content/themes/canvas/css/bootstrap.css" type="text/css" />
<link rel="stylesheet" href="http://www.website.com/wp-content/themes/canvas/style.css" type="text/css" />
<link rel="stylesheet" href="http://www.website.com/wp-content/themes/canvas/css/dark.css" type="text/css" />
<link rel="stylesheet" href="http://www.website.com/wp-content/themes/canvas/css/font-icons.css" type="text/css" />
<link rel="stylesheet" href="http://www.website.com/wp-content/themes/canvas/css/animate.css" type="text/css" />
<link rel="stylesheet" href="http://www.website.com/wp-content/themes/canvas/css/magnific-popup.css" type="text/css" />
<link rel="stylesheet" href="http://www.website.com/wp-content/themes/canvas/css/responsive.css" type="text/css" />

如何将所有这些 css 文件正确加载到 WordPress 页面中。 谢谢! 我目前正在使用以下代码

    define("THEME_DIR", get_template_directory_uri());
/*--- REMOVE GENERATOR META TAG ---*/
remove_action('wp_head', 'wp_generator');

// ENQUEUE STYLES

function enqueue_styles() {

    /** REGISTER css**/
    wp_register_style( 'bootstrap', THEME_DIR . '/css/bootstrap.css', array(), '1', 'all' );
    wp_register_style( 'dark', THEME_DIR . '/css/dark.css', array(), '1', 'all' );
    wp_register_style( 'font-icons', THEME_DIR . '/css/font-icons.css', array(), '1', 'all' );
    wp_register_style( 'animate', THEME_DIR . '/css/animate.css', array(), '1', 'all' );
    wp_register_style( 'magnific-popup', THEME_DIR . '/css/magnific-popup.css', array(), '1', 'all' );
    wp_register_style( 'responsive', THEME_DIR . '/css/responsive.css', array(), '1', 'all' );
    //wp_register_style( 'style', THEME_DIR . '/style.css', array(), '1', 'all' );

    /** ENQUEUE css**/
    wp_enqueue_style( 'bootstrap' );
    wp_enqueue_style( 'dark' );
    wp_enqueue_style( 'font-icons' );
    wp_enqueue_style( 'animate' );
    wp_enqueue_style( 'magnific-popup' );   
    wp_enqueue_style( 'responsive' );
    //wp_enqueue_style( 'style' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_styles' );

// ENQUEUE SCRIPTS

function enqueue_scripts() {

    wp_register_script( 'html5-shim', 'http://html5shim.googlecode.com/svn/trunk/html5.js', array( 'jquery' ), '1', false );
    wp_enqueue_script( 'html5-shim' );


    wp_register_script( 'custom-script', THEME_DIR . '/js_path/customscript.js', array( 'jquery' ), '1', false );
    wp_enqueue_script( 'custom-script' );

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

但这不起作用...我还检查了 wp_head() 是否存在于我的头文件中,并且所有 php 都在工作??? 我一添加 style.css 就会卡在加载中

【问题讨论】:

  • 你必须找到你的css文件的正确路径。您应该检查代码并在控制台部分查看是否有错误消息。
  • 请注意,引导 JavaScript 不能直接与 jQuery 提供的 ba WordPress 一起使用。通过使用现有的引导框架主题并将您的主题创建为它的子主题,您可以省去很多麻烦。
  • 我没有使用 bootstrap javascript 我正在使用 bootstrap css ...尝试使用 cdn 并且效果很好我的问题在于包含 css 文件

标签: html css wordpress


【解决方案1】:

一般来说,为样式表句柄使用唯一名称是个好主意(请参阅reference)。我无法重现错误,但您可以尝试更改

wp_register_style( 'style', THEME_DIR . '/style.css', array(), '1', 'all' );

到

wp_register_style( 'unique-name-style', THEME_DIR . '/style.css', array(), '1', 'all' );

和

wp_enqueue_style( 'style' );

到

wp_enqueue_style( 'unique-name-style' );

【讨论】:

    【解决方案2】:

    您不应将 CSS 和 JS 文件直接包含在您的 WordPress 模板中。相反,您应该通过wp_enqueue_style() 正确地将它们排入队列(在functions.php 中):

    /**
     * Proper way to enqueue scripts and styles
     */
    function theme_name_scripts() {
        wp_enqueue_style( 'style-name', get_stylesheet_directory_uri() . '/css/bootstrap.css' );
        wp_enqueue_script( 'script-name', get_stylesheet_directory_uri() . '/js/example.js', array(), '1.0.0', true );
    }
    add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
    

    【讨论】:

    • 我也可以这样加载 style.css 吗?
    • 是的,wp_enqueue_style( 'default-styles', get_stylesheet_uri() );。我的答案中的链接中谈到了这一点。
    • THEME_DIR 之后缺少正斜杠... get_template_directory_uri() 函数返回不带斜杠的 uri。因此,您需要使用 THEME_DIR . '/css/bootstrap.css' 之类的路径
    • 是的,我修复了正斜杠的问题……但我添加 style.css 后仍然卡在加载中??
    【解决方案3】:

    在默认主题二十十五中查看functions.php。可能会想到:

    <?php /**
     * Enqueue scripts and styles.
     *
     * @since Twenty Fifteen 1.0
     */
    function twentyfifteen_scripts() {
      // Add custom fonts, used in the main stylesheet.
      wp_enqueue_style( 'twentyfifteen-fonts', twentyfifteen_fonts_url(), array(), null );
    
      // Add Genericons, used in the main stylesheet.
      wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.2' );
    
      // Load our main stylesheet.
      wp_enqueue_style( 'twentyfifteen-style', get_stylesheet_uri() );
    
      // Load the Internet Explorer specific stylesheet.
      wp_enqueue_style( 'twentyfifteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentyfifteen-style' ), '20141010' );
      wp_style_add_data( 'twentyfifteen-ie', 'conditional', 'lt IE 9' );
    
      // Load the Internet Explorer 7 specific stylesheet.
      wp_enqueue_style( 'twentyfifteen-ie7', get_template_directory_uri() . '/css/ie7.css', array( 'twentyfifteen-style' ), '20141010' );
      wp_style_add_data( 'twentyfifteen-ie7', 'conditional', 'lt IE 8' );
    
      wp_enqueue_script( 'twentyfifteen-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20141010', true );
    
      if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
        wp_enqueue_script( 'comment-reply' );
      }
    
      if ( is_singular() && wp_attachment_is_image() ) {
        wp_enqueue_script( 'twentyfifteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20141010' );
      }
    
      wp_enqueue_script( 'twentyfifteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20141212', true );
      wp_localize_script( 'twentyfifteen-script', 'screenReaderText', array(
        'expand'   => '<span class="screen-reader-text">' . __( 'expand child menu', 'twentyfifteen' ) . '</span>',
        'collapse' => '<span class="screen-reader-text">' . __( 'collapse child menu', 'twentyfifteen' ) . '</span>',
      ) );
    }
    add_action( 'wp_enqueue_scripts', 'twentyfifteen_scripts' );
    ?>
    

    特别是,如果您有一个带有该样式的文件,请将其连接为默认:

    // Load our main stylesheet.
      wp_enqueue_style( 'main-style', get_stylesheet_uri() );
    

    这样它就会连接到你的主题的 style.css 的根目录中

    【讨论】:

    • twentyfifteen 与 OP 遇到的问题有什么关系?
    • 如果您为 IE 或其他添加额外的样式或样式...您可以从默认主题 25 的 function.php 中获取示例
    【解决方案4】:

    请在您主题的functions.php文件中使用此代码

    function theme_scripts() {
        wp_enqueue_script('jquery');
        wp_enqueue_script('respond', get_template_directory_uri()."/js/custom.js", '', null, true );
    
        wp_enqueue_style ( "theme", get_bloginfo('stylesheet_url'), '', null );
        wp_enqueue_style ( "bootstrap", get_template_directory_uri()."/css/custom.css", '', null );
    
    }
    add_action( 'wp_enqueue_scripts', 'theme_scripts' );
    

    【讨论】:

      【解决方案5】:

      style.css 没有被包含或卡在背景底色上。使用1.0.0 否则它会认为它是一个布尔语句。

      改变

      wp_register_style( 'dark', THEME_DIR . '/css/dark.css', array(), '1', 'all' );
      

      到

      wp_register_style( 'dark', THEME_DIR . '/css/dark.css', array(), '1.0.0', 'all' );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-25
        • 2014-12-07
        • 2016-07-29
        相关资源
        最近更新 更多