Add Custom CSS to a Page or Post (With and Without Plugins)

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
3.7 (73.33%) 3 votes

Recently, I had to add some custom CSS to a certain page and I found it useful to share how to do it with and without plugins.

There are many ways to conditionally enqueue a stylesheet to a certain page or post. I’ll explain how to do that by using a simple plugin and by writing a snippet for the people who want to do their stuff without plugins.

Add Custom CSS to a Page or Post using a Plugin

I’ve found this super easy plugin, WP Add Custom CSS. You can use it to add custom CSS to the whole website. Also, you can use it to add conditional styles to individual posts, pages, and custom post types.

Once installed and activated, you will find a custom CSS box added to your dashboard when you edit any of your posts or pages.

Add Custom CSS to a Page or Post Without Plugins

The code I used is a combination of wp_enqueue_style and Conditional Tags. Simply, you enqueue the style if the conditional tag is met such as page ID, post ID, page template etc.

First, you will need to add your custom CSS to a .css file. Preferrably in a child theme. In the following example, I added the custom CSS to home.css.
Then, I used this snippet in my child theme’s functions.php file. You’ll need to change PAGE_ID to your actual page ID or page name.

Again, you can replace is_page() with your desired conditional tag. For posts, you can use is_single(). For a specific page template, you can use something like is_page_template( 'templates/about.php' ) and so on.

READ  How to Customize WordPress Themes Like A Pro? Ultimate Guide for Beginners

I hope you find this useful. If you have any feedback please feel free to use the comments section below. Thanks for reading!

(Visited 1,419 times, 1 visits today)

Nagdy

I write about Multilingual WordPress, SEO, Website Performance. Also, I like answering WordPress question on Quora. When I have the time, I run online sessions to teach WordPress for my personal FB page fans!

felis ut amet, elit. eget mattis Phasellus

Pin It on Pinterest

Do you find this interesting?

Share it with your friends!