I want my single post to have a style different from the main style. I know we can include a custom CSS file in single.php
but I do not have a single.php
file in my theme!
I just want a custom id, like id="single"
, added to the single post (not in main page posts) and then I can write styles for it.
I found some PHP code but I don't know how I can use it, or if it is what I want or not...
if (is_single()) {
$single_css_styling = get_post_meta($post->ID, 'single_css_styling', true);
if (!empty($single_css_styling)) { ?>
<style type="text/css">
<?php echo $single_css_styling; ?>
<style>
<?php } }
Is there any way to do it?
<?php if (is_single()) {
echo '<script type="text/javascript">
jQuery(document).ready(function(){jQuery("body").addClass("single");});
</script>';
} ?>
you can now class >> single is added to your body tag using that identity you can easily apply your css
Note : jQuery file must added to your site for that
Without jQuery
add_filter('body_class','my_class_names');
function my_class_names($classes) {
if (is_single()){
// add 'class-name' to the $classes array
$classes[] = 'single';
// return the $classes array
}
return $classes;
}
Please make sure in body tag of header.php
<body <?php body_class($class); ?>>
is putted or not
Your theme should use the standard body_class() WordPress method in the <body>
element, as shown in the example in the documentation:
<body <?php body_class($class); ?>>
That will automatically add useful classes to the body element, including single
if it's a single page. Then you just target your CSS at .single
.