0

How do you add CSS code to a certain blog post in WordPress? Would I have to create a new file and keep the CSS code I need for this blog post in that file?

2

You can use PHP code in your child themes functions.php file to add a custom body class to any page/post etc.

add_filter( 'body_class', 'my_custom_body_class' );
function my_custom_body_class( $classes ) {
if ( is_single( '007' ))
    $classes[] = 'custom-class';
    return $classes;
}

This sample code will add a custom body class to the post with i.d of 007 so you can style that specific post in your child themes style.css file.

Sample CSS:

.custom-class {
font-size: 20px;
}

Source http://codex.wordpress.org/Function_Reference/body_class

0

The easiest way is probably to add it in the post itself (inline CSS) if you are only editing ONE post (you will have to use the HTML editor, not the visual one), and the styling you need is not too extensive. If you are editing styles in a whole category or custom post type, for example, then you will need to look into creating Templates. If you follow the first option, your post content could look something like this:

In this article, we will explore <span style="color: red;">life</span>, <span style="color: blue;">death</span> and <span style="color: green">the inbetween</span>.
0

You can use custom.css to override the changes and target specific posts using the body tag.

If you look at the source code for a particular post it will show something like:

<body class="single single-post postid-1178 single-format-standard mp6 single-author highlander-enabled highlander-light" style="" data-gclp-initialized="true" data-gistbox-initialized="true" cz-shortcut-listen="true">

so in your css you can always do something like

body.postid-1178 .some-div {
color: #999;
}

Also you can do something like this

body:not(.postid-1178) .some-div {
    color: #999;
}

to affect everything other than the home

  • some (most?) themes do not support custom css – Mark Kaplun Dec 30 '13 at 5:21

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.