2015-07-20 61 views
0

我經歷了一個教程,創建在以下鏈接在WordPress的模塊化的CSS:functions.php文件無法過濾父主題

http://themeshaper.com/2009/04/30/modular-css-wordpress-child-themes/

當我使用的style.css從導入CSS文件它的工作原理。 這裏是在style.css中的代碼在子主題:

的style.css

/* CSS Document */ 
/* 
Theme Name: Chiron 
Description: Child theme of thematic 
Template: thematic 
*/ 

/* Using @import, we can borrow style sheets from the Parent Theme */ 

/* Reset the browser defaults */ 
@import url('../thematic/library/styles/reset.css'); 

/* Apply default typography */ 
@import url('../thematic/library/styles/typography.css'); 

/* Add WordPress image styles */ 
@import url('../thematic/library/styles/images.css'); 

/* Add a basic layout */ 
@import url('../thematic/library/layouts/2c-l-fixed.css'); 

/* Start with some default styles */ 
@import url('../thematic/library/styles/18px.css'); 

但是當我刪除所有來自style.css的進口和使用的functions.php我的孩子裏面主題目錄來加載它不工作的CSS文件。

的functions.php

<?php 

function childtheme_create_stylesheet() { 
    $templatedir = get_bloginfo('template_directory'); 
    $stylesheetdir = get_bloginfo('stylesheet_directory'); 
    ?> 
    <link rel="stylesheet" type="text/css" href="<?php echo $templatedir ?>/library/styles/reset.css" /> 
    <link rel="stylesheet" type="text/css" href="<?php echo $templatedir ?>/library/styles/typography.css" /> 
    <link rel="stylesheet" type="text/css" href="<?php echo $templatedir ?>/library/styles/images.css" /> 
    <link rel="stylesheet" type="text/css" href="<?php echo $templatedir ?>/library/layouts/2c-l-fixed.css" /> 
    <link rel="stylesheet" type="text/css" href="<?php echo $templatedir ?>/library/styles/18px.css" /> 
    <link rel="stylesheet" type="text/css" href="<?php echo $stylesheetdir ?>/style.css" /> 

    <?php  
} 
add_filter('thematic_create_stylesheet', 'childtheme_create_stylesheet'); 

?> 

任何一個可以請建議我在哪裏做錯了嗎?

+1

在遵循極老的教程時要小心,在發佈堆棧溢出問題之前,請不要忘記Google解決您的問題。該教程已超過6年,並且[Google的第一個結果爲'thematic_create_stylesheet'](https://www.google.com.au/#q=thematic_create_stylesheet)解釋說,在Thematic 1.0中刪除了該鉤子(超過三幾年前),而應該使用'wp_enqueue_style'。 –

回答

1

你應該用正確的方法在樣式表(和腳本)的有關功能添加:

// Enqueue Scripts 
function my_scripts() { 
    wp_register_style('main', get_stylesheet_directory_uri() . '/assets/css/main.css'); 
    wp_register_style('anotherfile', get_stylesheet_directory_uri() . '/assets/css/main.css'); 
    wp_enqueue_style('main'); 
    wp_enqueue_style('anotherfile'); 
} 
add_action('wp_enqueue_scripts', 'my_scripts'); 

特別是作爲你有很多文件,這將允許你使用類似W3TC的東西,併合並和最小化文件。

您可以將此代碼添加到您的子主題functions.php文件中。

有關更多詳細信息,請參見https://codex.wordpress.org/Function_Reference/wp_register_stylehttps://codex.wordpress.org/Function_Reference/wp_enqueue_style

1

您可以嘗試使用的

href="<?php echo $templatedir; ?>/library/styles/reset.css 

代替

href="<?php echo $templatedir ?>/library/styles/reset.css 

但要確保$templatedir$stylesheetdir是給你正確的目錄。

還可以使用

add_action('admin_init', 'childtheme_create_stylesheet');

,而不是

add_filter('thematic_create_stylesheet', 'childtheme_create_stylesheet'); 

,強烈建議使用WordPress的默認功能。您可以點擊這裏查看此:

https://codex.wordpress.org/Function_Reference/add_editor_style

感謝

1

試試這個: 1。如果你想使用主題網址:

get_template_directory_uri();

  • ,如果你想使用孩子主題網址:
  • get_stylesheet_directory_uri();

    在的style.css(子主題):

    /* 
    Theme Name: twentyfifteen-child 
    Template: twentyfifteen 
    
    */ 
    

    在Function.php

    add_action('wp_enqueue_scripts', 'theme_enqueue_styles'); 
    function theme_enqueue_styles() { 
        echo get_template_directory_uri(); 
        echo get_stylesheet_directory_uri(); 
        wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css'); 
    
    } 
    

    試試這個:

    add_action('wp_enqueue_scripts', 'theme_enqueue_styles'); 
    function theme_enqueue_styles() { 
        echo get_template_directory_uri(); 
        echo get_stylesheet_directory_uri(); 
        wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css');  
        wp_enqueue_style('parent-stylereset', get_template_directory_uri() . '/library/styles/reset.css'); 
        wp_enqueue_style('parent-styletypo', get_template_directory_uri() . '/library/styles/typography.css'); 
        wp_enqueue_style('parent-styleimges', get_template_directory_uri() . '/library/styles/images.css'); 
        wp_enqueue_style('parent-style2c', get_template_directory_uri() . '/library/layouts/2c-l-fixed.css'); 
        wp_enqueue_style('parent-style18px', get_template_directory_uri() . '/library/styles/18px.css'); 
    
    }