• I’m using the code below to to create custom templates for single post categories. Now I want to use a specific css-file, but how do I call that to this.
    Thanks

    //Gets post cat slug and looks for single-[cat slug].php and applies it
    
    add_filter('single_template', create_function(
    	'$the_template',
    	'foreach( (array) get_the_category() as $cat ) {
    		if ( file_exists(TEMPLATEPATH . "/single-{$cat->slug}.php") )
    		return TEMPLATEPATH . "/single-{$cat->slug}.php"; }
    	return $the_template;' )
    );

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi there,

    That could get quite messy in your theme having all of those stylesheets. Is the goal just to have separate CSS for the different templates? If so, I’d look to see if WordPress adds a body class like .single-{$cat->slug} when the template is in use. And if so, use ones stylesheet and just prefix the CSS selectors with that body class.

    Thread Starter popmouth

    (@popmouth)

    Yes, I like separating css in different stylesheets for two reasons: 1. you don’t have to load ALL css for each page, 2. it’s easy when going back and doing changes.
    So usually I call them in the <head> in header.php like this:

    <?php if (is_page_template('kontakt.php')) { ?><link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/kontakt.css">
    	<?php } ?>

    , and I tried doing this, which didn’t work.:

    <?php if (is_single_template('single-foobar.php')) { ?><link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/single-foobar.css">
    	<?php } ?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Use custom css-file with single post template based on category’ is closed to new replies.