Php 如何在我的WordPress主题中使用wp_enqueue_style()?

Php 如何在我的WordPress主题中使用wp_enqueue_style()?,php,wordpress,Php,Wordpress,我正在为一个客户建立我的第一个WordPress网站。我真的很想使用CSS,并找到了一个名为的WP插件 现在,我是WordPress的新手,但这个插件似乎需要我使用一个名为的函数来告诉WordPress处理.less文件 我不知道在哪里使用这个函数。我在我的主题目录中查看了header.php文件,我看到了这个 <link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo( 'stylesheet_

我正在为一个客户建立我的第一个WordPress网站。我真的很想使用CSS,并找到了一个名为的WP插件

现在,我是WordPress的新手,但这个插件似乎需要我使用一个名为的函数来告诉WordPress处理.less文件

我不知道在哪里使用这个函数。我在我的主题目录中查看了header.php文件,我看到了这个

<link rel="stylesheet" type="text/css" media="all"
  href="<?php bloginfo( 'stylesheet_url' ); ?>" />

不完全是,但几乎是。您要做的是在
functions.php
中放置一个函数,对脚本进行排队

因此:


然后确保你有
do_action('wp_head')header.php
文件中使用code>,它应该可以正常工作。

主题或插件内部的wp\u enqueue\u风格用法:

wp_enqueue_style( 'my-style', get_template_directory_uri() . '/css/my-style.css', false, '1.0', 'all' ); // Inside a parent theme
wp_enqueue_style( 'my-style', get_stylesheet_directory_uri() . '/css/my-style.css', false, '1.0', 'all' ); // Inside a child theme
wp_enqueue_style( 'my-style', plugins_url( '/css/my-style.css', __FILE__ ), false, '1.0', 'all' ); // Inside a plugin

我自己也遇到了这个问题,埃曼的建议几乎奏效了。它可能是WordPress(3.4)的版本,虽然我不是php开发人员,所以我不确定,但我需要在函数下面使用它,而不是提供什么:

add_action('wp', 'useLess');

在theme function.php中添加下面的函数,您将获得样式和脚本

<?php
if ( ! function_exists( 'add_script_style' ) ) {
    function add_script_style() {
        /* Register & Enqueue Styles. */

        wp_register_style( 'my-style', get_template_directory_uri().'/css/my-style.css' );
        wp_enqueue_style( 'my-style' );

        /* Register & Enqueue scripts. */

        wp_register_script( 'my-script', get_template_directory_uri().'/js/my-script.js' );
        wp_enqueue_script( 'my-script');
    }
}
add_action( 'wp_enqueue_scripts', 'add_script_style', 10 );
?>

如果正确输入此代码,您必须看到index.php文件wp_head();&wp_footer(); 如果不存在,则需要在索引文件中添加此函数。您需要添加此wp_头();在haed标记之前,另一个在body标记之前添加

函数加载\u style(){

wp_register_style('my_style',get_template_directory_uri()。/css/my_style.css');
wp_enqueue_风格(“我的风格”);
}
//注册样式表。
添加动作(“wp_排队_脚本”、“加载_样式”)


更多信息

是的,但我的标题不是主页独有的。因此,如果我将它添加到我的标题中,它包含在每个页面中。标记,那么您将把
wp\u enqueue\u script()
调用放在
if(is\u front\u page()){…}
条件语句中。这将导致脚本只添加到主页上,而不是每一步。欢迎使用SO!您的答复只是代码,所以请编辑并给出代码格式。此外,当有其他答案时,您应该用代码注释一个答案,以便最大限度地解释它。
add_action('wp', 'useLess');
<?php
if ( ! function_exists( 'add_script_style' ) ) {
    function add_script_style() {
        /* Register & Enqueue Styles. */

        wp_register_style( 'my-style', get_template_directory_uri().'/css/my-style.css' );
        wp_enqueue_style( 'my-style' );

        /* Register & Enqueue scripts. */

        wp_register_script( 'my-script', get_template_directory_uri().'/js/my-script.js' );
        wp_enqueue_script( 'my-script');
    }
}
add_action( 'wp_enqueue_scripts', 'add_script_style', 10 );
?>