0

I am trying to link my css file to WordPress through a functions.php file. I am not getting any errors but my page isn't showing up styled at all. Am I missing something?

Here is my code:

<?php

function my_own_styles() {

wp_enqueue_style( 'portfolio_theme', get_template_directory_uri() . 
'/css/portfolio.css' );

}

add_action( 'wp_enqueue_scripts', 'my_own_styles()' );

?> 

2 Answers 2

1

When delcaring a function in the add_action function you don't need to use the (), just the name of the function. See how your code should look below.

<?php

function my_own_styles() {

wp_enqueue_style( 'portfolio_theme', get_template_directory_uri() . 
'/css/portfolio.css' );

}

add_action( 'wp_enqueue_scripts', 'my_own_styles' );

?> 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! I totally didn't see that! But unfortunately it is still not linked. Any other ideas?
@SeanFF I am not sure what else to suggest as that should work. The only other thing I can suggest is that the file doesn't exist in the themes css directory. Do you get any errors when you look at your browser developer console?
No, Nothing at all. That's the frustrating part. I wish I had errors in there. They would be a big help right now. Well, Thanks so much for that () catch! Truly appreciate it
1

If you are using a child theme - which I assume you are when you're making changes such as adding CSS and changing functions.php - then you need to use get_stylesheet_directory_uri instead of get_template_directory_uri, i.e.

function my_own_styles() {

    wp_enqueue_style( 'portfolio_theme', get_stylesheet_directory_uri() . 
'/css/portfolio.css' );

}

add_action( 'wp_enqueue_scripts', 'my_own_styles' );

get_template_directory_uri() returns the path to the current theme unless a child them is being used. In that case it returns the path to the parent theme.

get_stylesheet_directory_uri returns the path to the current theme even if it is a child theme.

2 Comments

Thanks for the help! But for some reason it still isn't linked. What else could be missing? I can't seem to find out what's wrong.
@SeanFF Can you check the generated HTML and see 1. if a link for portfolio.css is being added (it might just be using the wrong url) and 2.if it's being added, is it before or after your themes style.css? Also, can you update your question to include all your wp_enqueue_scripts (i.e. style.css etc)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.