Create and call for custom CSS file in CPT

Hello, I’ve tried a lot of things and googled, and can’t do this…

I made a CPT type and changed my content.php file (to content-cpt.php) in child theme. While I built it, I added the CSS to this file. Now I want to remove that CSS and enqueue it from its own file. Can you please tell me how to do this? I put the style-cpt.css in the same folder as my content-cpt.php. I tried calling it with _content-post-header.php and also from the content-cpt.php with no luck.

Thank you!

Hi @3butterflies,

Thanks for reaching out.

Creating a content-cpt.php isn’t enough, please check this for more details https://theme.co/apex/forums/topic/using-cornerstone-to-create-custom-post-type-page-template/#post-639347. And check this about FTP and editing https://theme.co/apex/forum/t/how-to-upload-xtheme-to-child-theme/43246/2

Thanks!

That does not answer my question. I know that content-cpt.php is not all you do. I have my custom post working exactly how I want it. I was just saying that most of my custom changes were performed inside of the content-cpt.php.

My question is, how do I get the CSS I need for my custom post type called up for that file? It does not work to put it in style.css. I could not get it to work by enqueueing it. It DOES work when it is simply added to the end of my content-cpt.php file within some tags.

Hi There,

In this case, you should enqueue a CSS file for your custom post type. Please add this custom code under functions.php locates in your child theme:

add_action('wp_enqueue_scripts', 'x_enqueue_scripts');
function x_enqueue_scripts() {
	if(is_singular( 'cpt' )){
		wp_enqueue_style( 'custom-cpt', get_stylesheet_directory_uri() . '/css/cpt.css', array(), false, 'all' );
	}
}
  • Don’t forget to create a cpt.css file under x-child/css.
  • In this code is_singular( 'cpt' ), the cpt is your custom post type slug.

For more information, please take a look at these articles:

Hope it helps :slight_smile:

Thank you very much!

Glad we were able to help you :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.