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

jes*_*vin 20 php wordpress wordpress-plugin

我正在为客户构建我的第一个WordPress网站.我真的想在CSS中使用LESS,并找到一个名为WP-LESS的WP插件.

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

我无法弄清楚我在哪里使用这个功能.我查看了我的主题目录中的header.php文件,我看到了这一点.

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

我应该用这样的代码替换这段代码吗?

<?php wp_enqueue_style('mytheme',
  get_bloginfo('template_directory').'/style.less',
  array('blueprint'), '', 'screen, projection'); ?>
Run Code Online (Sandbox Code Playgroud)

EAM*_*ann 25

不完全,但差不多.你想要做的是在functions.php你的脚本排队中放置一个函数.

所以:

function addMyScript() {
    wp_enqueue_style('mytheme', get_bloginfo('template_directory').'/style.less', array('blueprint'), '', 'screen, projection');
}
add_action('wp_head', 'addMyScript');
Run Code Online (Sandbox Code Playgroud)

然后确保你有do_action('wp_head');你的header.php文件,它应该工作得很好.


lim*_*cat 24

主题或插件中的wp_enqueue_style用法:

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


小智 5

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

add_action('wp', 'useLess');
Run Code Online (Sandbox Code Playgroud)


Muk*_*hal 5

在您的主题 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 );
?>
Run Code Online (Sandbox Code Playgroud)