代码之家  ›  专栏  ›  技术社区  ›  jessegavin

如何在我的WordPress主题中使用wp\u enqueue\u style()?

  •  20
  • jessegavin  · 技术社区  · 15 年前

    我正在为一个客户建立我的第一个WordPress网站。我真的很想用 LESS 找到了一个名为 WP-LESS .

    现在,我完全WordPress新手,但似乎这个插件需要我使用一个函数称为 wp_enqueue_style() 告诉WordPress处理.less文件。

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

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

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

    <?php wp_enqueue_style('mytheme',
      get_bloginfo('template_directory').'/style.less',
      array('blueprint'), '', 'screen, projection'); ?>
    
    4 回复  |  直到 15 年前
        1
  •  25
  •   EAMann    15 年前

    functions.php

    所以:

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

    那就确保你有 do_action('wp_head'); 在你的 header.php

        2
  •  25
  •   lime-cat    13 年前

    wp\u在主题或插件中加入\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
    
        3
  •  4
  •   Chris Bauer    13 年前

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

    add_action('wp', 'useLess');
    
        4
  •  3
  •   Mukesh Panchal    10 年前

    在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 );
    ?>
    
        5
  •  0
  •   Junaid Siddique    6 年前

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



    wp\u register\u style('my\u style',get\u template\u directory\u uri()。'/css/my_style.css');

    }
    //注册样式表。
    add_action('wp_enqueue_scripts','load_style');

    更多 details