Include style.css in the Child Theme with PHP

admin2025-06-06  0

In is written that the best practice for including style.css in the Child Theme one must put the code below into the functions.php of your Child Theme but replacing the 'parent-style' with the parametr of your Parent Theme which can be found in the functions.php of your Parent Theme. I couldn't find that in the file of my Theme, and there is not much of a code there. The method to include the Parent Theme stylesheet using @import is not working for me, the css that put in the file are not displaying themselves on the site in any browser.

function my_theme_enqueue_styles() {

    $parent_style = 'parent-style'; // This is 'twentyfifteen-style' for the Twenty Fifteen theme.

    wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array( $parent_style ),
        wp_get_theme()->get('Version')
    );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );

And I have one working example of this enqueue but for a different theme:

/**
 * Enqueue the parent theme stylesheet.
 */

add_action( 'wp_enqueue_scripts', 'vantage_parent_style' );
function vantage_parent_style() {
    wp_enqueue_style( 'parent-theme', get_template_directory_uri() . '/style.css' );
}

/**
 * Enqueue the child theme stylesheet.
 */

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

The name of my Theme is Samsklad and I don't know how to implement the code within my Child Theme's functions.php, I'm no good with PHP.

In https://codex.wordpress/Child_Themes is written that the best practice for including style.css in the Child Theme one must put the code below into the functions.php of your Child Theme but replacing the 'parent-style' with the parametr of your Parent Theme which can be found in the functions.php of your Parent Theme. I couldn't find that in the file of my Theme, and there is not much of a code there. The method to include the Parent Theme stylesheet using @import is not working for me, the css that put in the file are not displaying themselves on the site in any browser.

function my_theme_enqueue_styles() {

    $parent_style = 'parent-style'; // This is 'twentyfifteen-style' for the Twenty Fifteen theme.

    wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array( $parent_style ),
        wp_get_theme()->get('Version')
    );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );

And I have one working example of this enqueue but for a different theme:

/**
 * Enqueue the parent theme stylesheet.
 */

add_action( 'wp_enqueue_scripts', 'vantage_parent_style' );
function vantage_parent_style() {
    wp_enqueue_style( 'parent-theme', get_template_directory_uri() . '/style.css' );
}

/**
 * Enqueue the child theme stylesheet.
 */

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

The name of my Theme is Samsklad and I don't know how to implement the code within my Child Theme's functions.php, I'm no good with PHP.

Share Improve this question asked Nov 2, 2018 at 13:41 Brgerg30879Brgerg30879 74 bronze badges 4
  • How does your parent theme enqueue its stylesheet? Search for occurences of style.css in its files (presumably the functions.php). Can you post these in your question? – kero Commented Nov 2, 2018 at 13:45
  • I found it in the header.php - <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/style.css" type="text/css" media="screen"/> – Brgerg30879 Commented Nov 2, 2018 at 14:00
  • Ok, this means your theme wasn't developed using the WordPress best practices and you should disregard those articles. Either overwrite the header.php and use the suggested code (the handle (parent-style, twentyfifteen-style, ...) does not matter then) – kero Commented Nov 2, 2018 at 14:52
  • You mean put the first code into functions.php as it is? – Brgerg30879 Commented Nov 2, 2018 at 15:09
Add a comment  | 

1 Answer 1

Reset to default 1

You posted in a comment that the current theme uses the following code in its header.php

<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/style.css" type="text/css" media="screen"/>

This is not WordPress best practice and hence the linked tutorials fail you.

How to go on from this?

Copy header.php from parent to child theme, remove the line containing style.css, then you can place the following code in your child theme's functions.php:

function my_theme_enqueue_styles() {
    wp_register_style( 'parent-style', get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array( 'parent-style' ),
        wp_get_theme()->get('Version')
    );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749210810a317289.html

最新回复(0)