将其他CSS文件添加到wp_head [英] Adding other css files to wp_head

查看:75
本文介绍了将其他CSS文件添加到wp_head的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编辑最新的wordpress附带的标准二十三主题.

I am editing the standard twentythirteen theme that comes with the latest wordpress.

我需要将一些自己的.css文件添加到wp_head中,但是我不确定如何执行此操作.我目前正在wp_head之外调用我的文件,但这很杂乱,想正确执行.

I need to add a few of my own .css files into the wp_head but I'm not sure how to do this. I'm currently calling my files outside of wp_head but this is messy and would like to do it properly.

<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width">
<title><?php wp_title( '|', true, 'right' ); ?></title>
<link rel="profile" href="http://gmpg.org/xfn/11">
<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>">
<!--[if lt IE 9]>
<script src="<?php echo get_template_directory_uri(); ?>/js/html5.js"></script>
<![endif]-->
<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo("template_url"); ?>/bootstrap.css" />
<script type="text/javascript"   src="<?php bloginfo("template_url"); ?>/js/bootstrap.min.js"></script>

<?php wp_head(); ?>

在哪里定义wp_head的内容以及如何添加我自己的内容?

Where is it being defined what goes into wp_head and how do I add my own to it?

推荐答案

要在wp_head()中添加自己的CSS,您需要使用一系列WordPress函数:

To add your own css in the wp_head(), you need to use a collection of WordPress functions:

首先,将其放在主题的functions.php文件中:

First, you'll put this in your theme's functions.php file:

add_action('wp_enqueue_scripts', 'your_function_name');

(这使用添加操作钩子,并钩入

(This uses the add action hook, hooking into the wp_enqueue_scripts action.)

然后,您需要将该函数添加到将使用WordPress函数 wp_enqueue_style :

Then, you'll need to add the function to your functions.php file that will utilize the WordPress function wp_enqueue_style:

function your_function_name() {
    wp_enqueue_style('my-script-slug',  get_stylesheet_directory_uri() . '/your_style.css');
}

请注意 get_stylesheet_directory_uri()的使用-这将为您的主题获取正确的样式表目录.

Note the use of get_stylesheet_directory_uri() - this gets the proper stylesheet directory for your theme.

这也是将脚本排入标题的正确方法.示例:

This is also the proper way to enqueue scripts into your header. Example:

function your_function_name() {
    // Enqueue the style
    wp_enqueue_style('my-script-slug',  get_stylesheet_directory_uri() . '/your_style.css');
    // Enqueue the script
    wp_enqueue_script('my-script-slug',  get_stylesheet_directory_uri() . '/your_script.js');
}

使用WordPress wp_enqueue_script 函数.

Which uses the WordPress wp_enqueue_script function.

最后,值得一提的是,通常不建议直接更改二十三个(或任何其他核心主题)主题.建议创建一个儿童主题(我认为过分杀伤,但值得一提).

Finally, it is worth mentioning that altering the twenty thirteen (or any other core theme) theme directly is usually discouraged. The recommendation is to create a child theme (overkill in my opinion, but worth mentioning).

这篇关于将其他CSS文件添加到wp_head的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆