将其他css文件添加到wp_head

pro*_*est 25 php wordpress

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

我需要在wp_head中添加一些我自己的.css文件,但我不知道该怎么做.我目前在wp_head之外调用我的文件,但这很麻烦,并且想要正确地完成它.

<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(); ?>
Run Code Online (Sandbox Code Playgroud)

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

ran*_*ame 40

要在wp_head()中添加自己的css,需要使用WordPress函数的集合:

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

add_action('wp_enqueue_scripts', 'your_function_name');

(这使用了add action hook,挂钩到wp_enqueue_scripts动作.)

然后,您需要将函数添加到您将使用WordPress函数wp_enqueue_style的 functions.php文件中:

function your_function_name() {
    wp_enqueue_style('my-script-slug',  get_stylesheet_directory_uri() . '/your_style.css');
}
Run Code Online (Sandbox Code Playgroud)

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

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

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');
}
Run Code Online (Sandbox Code Playgroud)

其中使用WordPress wp_enqueue_script函数.

最后,值得一提的是,通常不鼓励直接改变二十三(或任何其他核心主题)主题.建议是创建一个儿童主题(在我看来有点矫枉过正,但值得一提).