Embed CSS in Post: Using A Custom Field

StevenWordPress

You may need to style a specific post with some custom css styles. Of course, you can just embed CSS in your post code, but as for many things, there is a fast way and there is a clean way.

If you want to embed CSS in your post in a clean way, you should use a custom field.

First things first: open your header.php file in your theme folder and insert the following code between the <head> and </head> html tags:

<?php if (is_single()) {
  $customcss = get_post_meta($post->ID, 'css', true);
  if (!empty($customcss)) { ?>
    <style type='text/css'>
      <?php echo $customcss; ?>
    </style>
  <?php }
} ?>

Once the file is saved, when you are publishing a post or a static page that needs some custom CSS styles, you just need to create a custom field named css and paste the required CSS in it.

Learn how I made $33,543 online in the past 12 months.

Simply input your best email below to get started.

Learn how I made $33,543 online in the past 12 months.

Simply input your best email below to get started.

No thanks, I have enough money