csswordpresschild-theming

How to enqueue multiple style sheets into my child theme in wordpress


I am fairly new to Wordpress but I do understand child themes.
This is the first time I came across a theme with a plugin that has its own style sheet enqueued. Is the following code adding the stylesheet of the booking plugin into the main parent style sheet already (so I do not need to include this in the child theme), or is this an additional enqueued style sheet and I will have to also include that separately in my child's theme.

Any explanation as to what exactly and how this code is working with the parent style would be appreciated. Wordpress codex is practically useless in this area.

{
wp_enqueue_style( 'villagio-style', get_stylesheet_uri(), array(), 
villagio_get_theme_version() );
    if ( is_plugin_active( 'motopress-hotel-booking/motopress-hotel-
booking.php' ) ) {
    wp_enqueue_style( 'villagio-motopress-hotel-booking', 
get_template_directory_uri() . '/css/motopress-hotel-booking.css', 
array('villagio-style'), villagio_get_theme_version(), 'all' );
}

Solution

  • Yes, you can put that code into your child theme functions.php file. It'll call motopress-hotel-booking.css file, when the plugin motopress-hotel-booking.php will be active. Here is full code:

    function custom_add_css_for_theme()
    {
        wp_enqueue_style('villagio-style', get_stylesheet_uri(), array(), villagio_get_theme_version());
    
        if (is_plugin_active('motopress-hotel-booking/motopress-hotel-booking.php')) {
            wp_enqueue_style('villagio-motopress-hotel-booking', get_template_directory_uri() . '/css/motopress-hotel-booking.css', array('villagio-style'), villagio_get_theme_version(), 'all');
        }
    }
    add_action( 'wp_enqueue_scripts', 'custom_add_css_for_theme' );
    

    It's don't matter, from where you called your function( from child theme, or from parent theme ). The function get_template_directory_uri() returns directory of your parent theme, even if you called it from child theme.