Skip to main content
Fixed the code. Works now. Tested.
Source Link

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($count$tag != 0null) {
    $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($t$tag == $count) {
            // If not, link the standard CSS file, but only if on the last loop.
            echo "<link rel='stylesheet' href='" . $dir . "generichref='generic-stylesheet.css' type='text/css'>";
        }
    }
}
  ?>

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

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($count != 0) {
    $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 . ".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($t == $count) {
            // If not, link the standard CSS file, but only if on the last loop.
            echo "<link rel='stylesheet' href='" . $dir . "generic-stylesheet.css' type='text/css'>";
        }
    }
}
 ?>

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

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.

added 1004 characters in body
Source Link

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($count != 0) {
    $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 . ".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($t == $count) {
            // If not, link the standard CSS file, but only if on the last loop.
            echo "<link rel='stylesheet' href='" . $dir . "generic-stylesheet.css' type='text/css'>";
        }
    }
}
?>

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

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).

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($count != 0) {
    $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 . ".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($t == $count) {
            // If not, link the standard CSS file, but only if on the last loop.
            echo "<link rel='stylesheet' href='" . $dir . "generic-stylesheet.css' type='text/css'>";
        }
    }
}
?>

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

Source Link

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).