【问题标题】:Functions in Wordpress function.phpWordpress 中的函数 function.php
【发布时间】:2015-10-15 11:36:53
【问题描述】:

我已经开始为 Wordpress 构建主题。添加某个点我想为网站添加一些样式。将样式添加到标题时,我可以让它工作,但我想使用 function.php 文件来完成此操作。这是我得到的:

<?php
function addCss(){
    wp_enqueue_style('style', get_template_directory_uri() . 'style.css');
}

add_action("wp_enqueue_scripts", "addCss");
?>

这是一个非常简单的函数,但由于某种原因,该函数没有被触发。我也没有收到任何错误,并且 style.css 没有根据控制台添加到网站。

我的文件结构:

我已尝试寻找解决方案,但找不到任何解决方案。我希望你们能帮助我。

【问题讨论】:

    标签: php css wordpress wordpress-theming


    【解决方案1】:

    确保在结束 &lt;/head&gt; 标记之前在 header.php 中调用 &lt;?php wp_head(); ?&gt;

    function addMyStyle() {
        wp_enqueue_style( 'my-style', get_template_directory_uri() . '/style.css', false, '1.0', 'all' );
    
    }
    add_action('wp_head', 'addMyStyle');
    

    【讨论】:

    • 很遗憾没有结果。我什至用你的代码替换了我的代码并添加了 就在 标记之前。
    • 它是经过测试的代码,它工作正常,请尝试禁用插件并检查您的文件是否正在加载。
    • 我成功了!您的解决方案很棒,但我需要将“wp_head”@add_action 更改为:“wp_enqueue_scripts”。感谢您的帮助!
    【解决方案2】:

    get_template_directory_uri() 返回一个没有“/”结尾的路径。

    在路径中添加“/”:

    // add this line to create a versionning of your stylesheet
    $ver = time();
    wp_enqueue_style('style', get_template_directory_uri() . '/style.css', $ver);
    

    希望对你有帮助!

    【讨论】:

    • 感谢您的回复。我按照你说的做了,但没有结果。
    • 您是否在 functions.php 中使用了您的函数,而不是在 header.php 中?也许删除 $ver : wp_enqueue_style('style', get_template_directory_uri() . '/style.css');
    【解决方案3】:

    你需要在 header.php 中有 wp_head()

     if ( ! function_exists( 'themescripts' ) ) {
        function themescripts() {
                wp_enqueue_style('style', get_stylesheet_uri(), array());
    
                wp_enqueue_script( 'jquery', array(), '', true);
                ....
            }
    
        add_action( 'wp_enqueue_scripts', 'themescripts' ); 
    }
    

    另外,请确保在 footer.php 文件中有 wp_footer()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-24
      • 1970-01-01
      • 1970-01-01
      • 2016-01-28
      • 1970-01-01
      • 1970-01-01
      • 2017-02-25
      相关资源
      最近更新 更多