WordPress - 排队脚本/ CSS的麻烦
So, I've not used Wordpress in a long time, and I'm trying to get back into the swing of things. Apparently the correct way to deal with external CSS/JS is to enqueue them using wp_register_style
, wp_enqueue_style
, wp_register_script
, and wp_enqueue_script
... but they're not working for me at all.
The following is my current functions.php
:
<?php
// Enqueue necessary scripts/etc.
function wasd_styles() {
wp_register_style('semantic-css', get_stylesheet_directory_uri() . '/lib/semantic.min.css');
wp_enqueue_style('semantic-css');
}
function wasd_scripts() {
wp_register_script('semantic-js', get_template_directory_uri() . '/lib/semantic.min.js');
wp_enqueue_script('semantic-js');
}
add_action('wp_enqueue_scripts', 'wasd_styles');
add_action('wp_enqueue_scripts', 'wasd_scripts');
?>
Is there something I'm doing wrong? As far as I can tell, it should be injecting the proper calls to grab CSS/JS into the tag of my page, but..it's not.
The following are the two files I'm attempting to render, I threw this together in like 5 minutes to attempt to test Semantic UI, but it's just not working at all, no style is being rendered, and is empty upon source inspection.
index.php
<?php get_header(); ?>
<div class="ui main container">
<h2 class="ui dividing header">This is a test</h2>
</div>
header.php
<div class="ui top fixed inverted menu">
<div class="item">Test</div>
</div>
所以,我很长一段时间没有使用过Wordpress,而我正试图回到秋千上 东西的。 显然,处理外部CSS / JS的正确方法是使用 以下是我当前的 有什么我做错了吗? 据我所知,应该注入正确的调用来抓取我的页面标签中的CSS / JS,但是......不是。 p>
以下是我的两个文件 我试图渲染,我在5分钟内把它扔在一起尝试测试语义UI,但它根本就没有工作,没有渲染样式,并且在源检查时是空的。 p>
index.php strong> p>
header.php strong> p>
wp_register_style code>,
wp_enqueue_style code>,
wp_register_script code>和
wp_enqueue_script 代码> ......但他们根本不适合我。 p>
functions.php code>: p>
&lt;?php
/ /排队必要的脚本/等。
函数wasd_styles(){
wp_register_style('semantic-css',get_stylesheet_directory_uri()。'/ lib /semantic.min.css');
wp_enqueue_style('semantic-css') ;
}
函数wasd_scripts(){
wp_register_script('semantic-js',get_template_directory_uri()。'/ lib /semantic.min.js');
wp_enqueue_script('semantic-js');
}
add_action('wp_enqueue_scripts','wasd_styles');
add_action('wp_enqueue_scripts','wasd_scripts');
?&gt;
code> pre>
&lt;?php get_header(); ?&gt;
&lt; div class =“ui main container”&gt;
&lt; h2 class =“ui divide header”&gt;这是一个测试&lt; / h2&gt;
&lt; / div&gt;
代码> pre>
&lt; div class =“ui top fixed inverted menu”&gt ;
&lt; div class =“item”&gt;测试&lt; / div&gt;
&lt; / div&gt;
code> pre>
div>
From the code you have shared, your theme does not support the use of wp_enqueue_script()
. This function simply registers the scripts and gets it ready for insertion into the DOM. This is inserted via a hook, which doesn't appear to be present in your theme. You would need to add the following into your header.php
file. This would typically be added between the <head></head>
tags.
<?php wp_head(); ?>
Also, you should have a similar hook in your footer.php
file, typically just before the closing </body>
tag. This hook would be:
<?php wp_footer(); ?>
Without these, wp_enqueue_script()
and wp_enqueue_style()
will not work.