1

i have tags, and for some tags i want to load a different stylesheet But i don't know how

i have....

function customstyles()
{
    if ( is_tag( 'circulair' ) ) {

        //Register and enqueue the stylesheet for tag-circulair.
        wp_register_style( 'tag-circulair', get_stylesheet_directory_uri() . '/layout-circulair' );  
        wp_enqueue_style( 'tag-circulair' );

    } else {

        //Register and enqueue the default stylesheet.    
        wp_register_style( 'styles', get_stylesheet_uri() );
        wp_enqueue_style( 'styles' );  

    }   
}
add_action( 'wp_enqueue_scripts', 'customstyles' );

But it won't do

Thanks Bets

3 Answers 3

0
function register_tag_styles() {
    wp_register_style( 'tag-circulair-template', get_stylesheet_directory_uri() . '/layout-circulair.css' );
    wp_register_style( 'tag-circulair-kantoor-template', get_stylesheet_directory_uri() . '/layout-circulair.css' );
    }
add_action( 'wp_enqueue_scripts', 'register_tag_styles' );

This code does the trick for me, but i think it's too complicated when i have more than 10 tags with different stylesheets.....

0

Try has_tag for it.

function customstyles() { 
  if ( has_tag( 'circulair' ) ) {
    //Register and enqueue the stylesheet for tag-circulair.
    wp_register_style( 'tag-circulair', get_template_directory_uri() . '/layout-circulair/style.css' );  
    wp_enqueue_style( 'tag-circulair' );
  } else {
    //Register and enqueue the default stylesheet.    
    wp_register_style( 'styles', get_stylesheet_directory_uri() );
    wp_enqueue_style( 'styles' );  
  }
}
add_action( 'wp_enqueue_scripts', 'customstyles' );

Hope this will helps you.

1
  • Thank you for your idea the one that does the trick is function register_tag_styles() { wp_register_style( 'tag-circulair-template', get_stylesheet_directory_uri() . '/layout-circulair.css' ); wp_register_style( 'tag-circulair-kantoor-template', get_stylesheet_directory_uri() . '/layout-circulair.css' ); } add_action( 'wp_enqueue_scripts', 'register_tag_styles' );
    – bets
    Commented Jan 3, 2017 at 13:50
0

What you might want to do instead is to get the tag in the header file of the theme and add the tag name as a class to one of the highest elements, for example .

That way, you can have a page tagged with "james", and say:

body.james #mainmenu li > a { color: #f00; }

Otherwise, you could get the tag and use php to try and load a stylesheet with the same name as the tag(or use a standard stylesheet if there are no tags, or file doesn't exist).

<?php
// Get the tags.
$tag = get_the_tags();
// Count the number of tags.
$count = count($tag);

// Check if there are tags at all.
if($tag != null) {
    $dir = get_stylesheet_directory() . "/";

    // Go through each tag.
    foreach($tag as $t) {
        // Get the full path to the CSS file we want to load based on the tag.
        $fullpath = $dir . $t->name . ".css";

        // Check if the file exists.
        if(file_exists($fullpath)) {
            // Link the CSS file.
            echo "<link rel='stylesheet' href='" . $fullpath . "' type='text/css'>";
        } else if($tag == $count) {
            // If not, link the standard CSS file, but only if on the last loop.
            echo "<link rel='stylesheet' href='generic-stylesheet.css' type='text/css'>";
        }
    }
} ?>

That should do the trick. This should go in your theme's/child theme's header file, after all the other CSS files have been loaded.

18
  • Thanks, but the css file is to big to load in the header
    – bets
    Commented Jan 3, 2017 at 10:56
  • Why is it too big to load in the header? Commented Jan 3, 2017 at 10:57
  • You right, is't not to big but it is to complicated because i have to do that for every tag that needs a different styleheet isn't it? And when i use php in the function, than it's easier. But i don't know how :)
    – bets
    Commented Jan 3, 2017 at 11:07
  • Using the second solution in my answer, you wouldn't need to do much manually. Basically: get tagname -> find tagname.css -> if exist, load / else load standard.css Commented Jan 3, 2017 at 12:27
  • This seems very simple Magnus do i write that code in function? and how do i write that?
    – bets
    Commented Jan 3, 2017 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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