functions.php文件无法过滤父主题

时间:2015-07-20 07:29:40

标签: wordpress

我正在通过以下链接在wordpress中创建模块化css的教程:

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');

?>

任何人都可以建议我在哪里做错了吗?

3 个答案:

答案 0 :(得分: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)

您应该使用正确的方法在样式表(和脚本)中添加相关功能:

// 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

答案 2 :(得分:1)

试试这个: 1.如果您想使用主题网址:

  

get_template_directory_uri();

  1. 如果您想使用主题网址:
  2.   

    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' );
    
    }
    
相关问题