2014-10-06 41 views
0

我有一個網站(http://lab.eksperimentar.com),我使用Delipress主題,它是小孩。 問題是:我在子主題內進行更改,頁面不加載styles.css。添加兒童主題css到<head>

我要 「插入」 到head標籤這個HTML元素:如果我打開子文件夾中的function.php並插入

<link rel="stylesheet" id="delipress-child-style-css" href="http://lab.eksperimentar.com/wp-content/themes/delipress-child/style.css" type="text/css" media="all"> 

<head> 
<link rel="stylesheet" id="delipress-style-css" href="http://lab.eksperimentar.com/wp-content/themes/delipress/style.css?ver=2.5.1" type="text/css" media="all"> 
</head> 

它breakes我的網站,因爲不是「插入」頭標籤,而是替代它。

如何在不篡改父主題的情況下做到這一點?

回答

0

沒有必要編輯functions.php文件;你應該打開header.php文件位於您的孩子主題文件夾內,然後添加<head></head>標記中的以下代碼:

<link rel="stylesheet" id="delipress-child-style-css" href="http://lab.eksperimentar.com/wp-content/themes/delipress-child/style.css" type="text/css" media="all">

+0

沒有我孩子的主題文件夾裏面'header.php'文件,只有一個'style.css'和' functions.php'。我將創建一個並讓您知道 – angelod1as 2014-10-06 22:43:53

+0

您應該創建一個位於您的父主題中的header.php文件的副本,然後將該複製的header.php文件移動到您的child-theme文件夾中。完成後,按照我在主要答案中提到的步驟進行操作。 – 2014-10-06 22:45:25

+0

作爲魅力! – angelod1as 2014-10-06 23:32:49

2

正確的方法是使用使用wp_register_style()功能,其次是wp_enqueue_style()

這兩個功能都需要掛鉤到wp_enqueue_script()

例如,從CODEX:

// Register style sheet. 
add_action('wp_enqueue_scripts', 'register_plugin_styles'); 

/** 
* Register style sheet. 
*/ 
function register_plugin_styles() { 
    wp_register_style('my-plugin', plugins_url('my-plugin/css/plugin.css')); 
    wp_enqueue_style('my-plugin'); 
} 

您也可以跳過註冊,直接使用wp_enqueue_style()單獨與同一鉤

讀也here當使用兒童主題..

add_action('wp_enqueue_scripts', 'load_my_child_styles', 20); 
function load_my_child_styles() { 
    wp_enqueue_style('child-theme', get_stylesheet_uri()); 
} 

另一個(不太推薦的)選項,如果你只有簡單的CSS,並且你想直接輸出到HEAD將使用wp_head()行動

<?php 
    function add_styles() 
    { 
     ?> 
     <style type="text/css"> 
     .menu ul li.world a { 
      background: url(<?php bloginfo('template_directory'); ?>/images/<?php echo get_option(THEME_PREFIX . 'intro_image'); ?>) repeat scroll 0%; 
     } 

     .m ul li.me a { 
      background: url(<?php bloginfo('template_directory'); ?>/images/<?php echo get_option(THEME_PREFIX . 'slider_image'); ?>) repeat scroll 0%; 
     } 

    </style> 
    <?php 
    wp_head 
    } 

    add_action('wp_head', 'add_styles'); ?> 

請注意,打開和關閉PHP標籤