0

My post template won't load the stylesheet, what's going wrong?

function wpse_enqueue_post_template_styles() {
    if ( is_post_template( 'category-bedrijven.php' ) ) {
        wp_enqueue_style( 'post-template', get_stylesheet_directory_uri() . '/layout-interieur.css' );
    }
}
add_action( 'wp_enqueue_scripts', 'wpse_enqueue_post_template_styles' );
  • 1
    you mean is_page_template or ? – Mohamed Rihan Dec 21 '16 at 12:01
1

You can do it like-

function wpse_enqueue_post_template_styles() {
    if ( is_category('bedrijven') ) {
        wp_enqueue_style( 'post-template', get_stylesheet_directory_uri() . '/layout-interieur.css' );
    }
}
add_action( 'wp_enqueue_scripts', 'wpse_enqueue_post_template_styles' );

Here bedrijven is your category slug.

As you said the above method hasn't worked for you, so here I'm suggesting another method-

First remove the first method's full code. Then follow this below method step by step-

Step 1: In your theme's functions.php put the below code block-

function the_dramatist_register_category_styles() {
    wp_register_style( 'post-template', get_stylesheet_directory_uri() . '/layout-interieur.css' );
}
add_action( 'wp_enqueue_scripts', 'the_dramatist_register_category_styles' );

By this above code block we are registering your stylesheet file at WordPress. So we can call it anytime.

Step 2: Now go to your category-bedrijven.php file and inside this file put the below code-

<?php wp_enqueue_style('post-template'); ?>

It'll must work if you do it right.

Hope that helps.

  • Thanx for your solution, but it didn't work for me unfortunately – bets Dec 21 '16 at 12:22
  • I tested it previously and I just tested it again. I worked both the times. Please inspect your page source and search for if layout-interieur.css file is called or not. @bets – CodeMascot Dec 21 '16 at 12:36
  • maybe its a taxonomy? – Mohamed Rihan Dec 21 '16 at 12:42
  • Nope. He already declared it in his question category-bedrijven.php. So how could it be another taxonomy ? @MohamedRihan – CodeMascot Dec 21 '16 at 12:43
  • I think @dramatist that you are right the layout-interieur.css isn't in mij page source i dont't know why, because he stands in the root directory and i don't know why he isn't loaded. – bets Dec 21 '16 at 12:58
0
function wpse_enqueue_post_template_styles() {

    if ( is_page_template( 'category-bedrijven.php' ) ) {
        wp_enqueue_style( 'cat-style', get_stylesheet_directory_uri() . '/layout-interieur.css' ); 
    } 
}
 add_action( 'wp_enqueue_scripts', 'wpse_enqueue_post_template_styles' );
  • Thanx for your solution but than it is an page and not a category post or am i wrong? – bets Dec 21 '16 at 12:33
  • if you php file name is category-bedrijven.php this my solution will work. category post mean single post or archive page? or custom post type? – Mohamed Rihan Dec 21 '16 at 12:37
  • sorry it's a category post – bets Dec 21 '16 at 14:56
0

You can do like this:

$handle = 'wpdocs';

wp_register_style( $handle,

get_stylesheet_directory_uri().'/relative/path/to/stylesheet.css', array(), '', true );

if ( is_page_template( 'template-name.php' ) ) { wp_enqueue_style( $handle ); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.