$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>themes - proper way to add css using functions.php?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

themes - proper way to add css using functions.php?

matteradmin9PV0评论

I asked this question before and the solution worked at that time when I asked! However it is not working anymore for some odd reason. I was hesitant to 'ask' the question again but I can't seem to find a solution. So what I am asking now is how to properly add css to a website using the functions.php.

Here is my functions.php code to add the css:

<?php 
    function fearnothing_script_enqueue(){
        wp_enqueue_style('customstyle',  get_template_directory_uri().'/css/fearnothing.css',array(), '1.1.1', 'all');


    }

    add_action('wp_enqueue_scripts', 'wp_enqueue_style');

Here is my fearnothing.css (i've been working on my website offline and the css works properly there so I don't know what the issue is with my functions.php file not adding css to my website):

html,
body {
    margin: 0;
    color: #91f213;
    background: black;
    font: sans-serif;
    cursor: pointer;
}

body{
    padding: 20px;
}
.video-container {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 100%;
    height: 100%; 
    overflow: hidden;
}
.video-container video {
    /* Make video to at least 100% wide and tall */
    min-width: 100%; 
    min-height: 100%; 

    /* Setting width & height to auto prevents the browser from stretching or squishing the video */
    width: auto;
    height: auto;

    /* Center the video */
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
}

header.php

<!DOCTYPE html>
<html>
<head>
    <title>lonely spaceship</title>
    <?php wp_head(); ?>
</head>
<!-- <div class="video-container">
        <video autoplay="true" muted="true" loop="true">
            <source src="wp-content/themes/fearnothing/earth.mp4" type="video/mp4" >
        </video> -->
</div>
<body>
    <p> This is a test text</p>

index.php

<?php get_header(); ?>

<p>Test index text</p>


<?php get_footer(); ?>

footer.php

        <footer>
            <p></p>
        </footer>
        <?php wp_footer(); ?>
    </body>
</html>

I asked this question before and the solution worked at that time when I asked! However it is not working anymore for some odd reason. I was hesitant to 'ask' the question again but I can't seem to find a solution. So what I am asking now is how to properly add css to a website using the functions.php.

Here is my functions.php code to add the css:

<?php 
    function fearnothing_script_enqueue(){
        wp_enqueue_style('customstyle',  get_template_directory_uri().'/css/fearnothing.css',array(), '1.1.1', 'all');


    }

    add_action('wp_enqueue_scripts', 'wp_enqueue_style');

Here is my fearnothing.css (i've been working on my website offline and the css works properly there so I don't know what the issue is with my functions.php file not adding css to my website):

html,
body {
    margin: 0;
    color: #91f213;
    background: black;
    font: sans-serif;
    cursor: pointer;
}

body{
    padding: 20px;
}
.video-container {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 100%;
    height: 100%; 
    overflow: hidden;
}
.video-container video {
    /* Make video to at least 100% wide and tall */
    min-width: 100%; 
    min-height: 100%; 

    /* Setting width & height to auto prevents the browser from stretching or squishing the video */
    width: auto;
    height: auto;

    /* Center the video */
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
}

header.php

<!DOCTYPE html>
<html>
<head>
    <title>lonely spaceship</title>
    <?php wp_head(); ?>
</head>
<!-- <div class="video-container">
        <video autoplay="true" muted="true" loop="true">
            <source src="wp-content/themes/fearnothing/earth.mp4" type="video/mp4" >
        </video> -->
</div>
<body>
    <p> This is a test text</p>

index.php

<?php get_header(); ?>

<p>Test index text</p>


<?php get_footer(); ?>

footer.php

        <footer>
            <p></p>
        </footer>
        <?php wp_footer(); ?>
    </body>
</html>
Share Improve this question edited Nov 28, 2018 at 12:16 asked Nov 28, 2018 at 12:07 user154455user154455 5
  • Does your theme have wp_head() in the header? – Jacob Peattie Commented Nov 28, 2018 at 12:12
  • Yes, <?php wp_head(); ?> – user154455 Commented Nov 28, 2018 at 12:13
  • please check get_template_directory_uri(). path working or not – vikrant zilpe Commented Nov 28, 2018 at 12:15
  • jacob is right answer please check – vikrant zilpe Commented Nov 28, 2018 at 12:18
  • Is this a child theme? – Tom J Nowell Commented Nov 28, 2018 at 12:20
Add a comment  | 

1 Answer 1

Reset to default 4

Your add_action() is incorrect:

add_action('wp_enqueue_scripts', 'wp_enqueue_style');

The 2nd argument is supposed to be the function that you enqueue your stylesheet in. In you case that's fearnothing_script_enqueue():

add_action( 'wp_enqueue_scripts', 'fearnothing_script_enqueue' );
Post a comment

comment list (0)

  1. No comments so far