2015-10-15 108 views
0

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

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

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

这是一个非常简单的函数,但由于某种原因,该函数没有被解雇。我也没有得到任何错误,并且style.css没有按照控制台添加到站点。

我的文件结构:

enter image description here

我试图寻找解决的办法,但找不到任何。我希望你们能帮助我。

回答

0

请确保您在header.php中呼叫<?php wp_head(); ?>,然后结束</head>标记。

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

} 
add_action('wp_head', 'addMyStyle'); 
+0

不幸的是没有结果。我甚至用你的代码替换了我的代码,并添加了<?php wp_head(); ?>在标签之前。 –

+0

它是测试代码,它工作正常,请尝试禁用插件并检查您的文件是否正在加载。 – Noman

+0

我懂了!您的解决方案非常棒,但我需要将'wp_head'@add_action更改为'wp_enqueue_scripts'。 Thx帮忙! –

0

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); 

我希望帮助你!

+0

感谢您的回复。我已经像你说过的,但没有结果。 –

+0

你是否在functions.php中使用你的函数,而不是在header.php中使用你的函数? 也许删除$ ver: wp_enqueue_style('style',get_template_directory_uri()。'/style.css'); – Nozifel

0

您需要的header.php

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'); 
} 

已经wp_head()另外,请务必在footer.php文件wp_footer()。

相关问题