如何将以下css代码添加到functions.php的页眉中?

ale*_*nco 0 css wordpress function

现在,我将以下代码放入header.php.

我认为解决方案不是很优雅.

如何将这个CSS代码添加functions.php到我的标题中(该代码将如何显示)?

    wp_head();
?>
<style>
    .jimgMenu ul li.landscapes a {
        background: url(<?php bloginfo('template_directory'); ?>/images/<?php echo get_option(THEME_PREFIX . 'intro_image'); ?>) repeat scroll 0%;
    }

    .jimgMenu ul li.people a {
        background: url(<?php bloginfo('template_directory'); ?>/images/<?php echo get_option(THEME_PREFIX . 'slider_image'); ?>) repeat scroll 0%;
    }

    .jimgMenu ul li.nature a {
        background: url(<?php bloginfo('template_directory'); ?>/images/nature.jpg) repeat scroll 0%;
    }
    .jimgMenu ul li.abstract a {
        background: url(<?php bloginfo('template_directory'); ?>/images/abstract.jpg) repeat scroll 0%;
    }

    .jimgMenu ul li.urban a {
        background: url(<?php bloginfo('template_directory'); ?>/images/urban.jpg) repeat scroll 0%;

    }
    .jimgMenu ul li.people2 a {
        background: url(<?php bloginfo('template_directory'); ?>/images/people.jpg) repeat scroll 0%;
        min-width:310px;
    }
</style>
Run Code Online (Sandbox Code Playgroud)

Ter*_*Ann 5

使用钩子是最好的方法 - 然后你只需修改functions.php而不是模板,如果作者发布更改,更新或补丁,更容易更新模板.

的functions.php

<?php

function add_styles()
{
    ?>
    <style type="text/css">
    .jimgMenu ul li.landscapes a {
        background: url(<?php bloginfo('template_directory'); ?>/images/<?php echo get_option(THEME_PREFIX . 'intro_image'); ?>) repeat scroll 0%;
    }

    .jimgMenu ul li.people a {
        background: url(<?php bloginfo('template_directory'); ?>/images/<?php echo get_option(THEME_PREFIX . 'slider_image'); ?>) repeat scroll 0%;
    }

    .jimgMenu ul li.nature a {
        background: url(<?php bloginfo('template_directory'); ?>/images/nature.jpg) repeat scroll 0%;
    }
    .jimgMenu ul li.abstract a {
        background: url(<?php bloginfo('template_directory'); ?>/images/abstract.jpg) repeat scroll 0%;
    }

    .jimgMenu ul li.urban a {
        background: url(<?php bloginfo('template_directory'); ?>/images/urban.jpg) repeat scroll 0%;

    }
    .jimgMenu ul li.people2 a {
        background: url(<?php bloginfo('template_directory'); ?>/images/people.jpg) repeat scroll 0%;
        min-width:310px;
    }
</style>

    <?php
}
add_action('wp_head', 'add_styles');
Run Code Online (Sandbox Code Playgroud)

假设你的主题是正确构建wp_head();<head>,并且在你的例子里,你不需要修改任何文件functions.php


我将添加对于网站负载优化和性能增强,因为客户端缓存外部样式表,你应该制作一个单独的样式表,然后代替我上面提到的功能打印出它将打印<link>到样式表的CSS .

虽然你可以完成不使用<?php bloginfo('template_directory'); ?>而不是使用相对网址(即../ images/.....)仍然会造成问题,get_option(THEME_PREFIX . 'intro_image')所以如果你的样式表更改真的很小我上面列出的使用钩子是一个好的解决方案,如果你要注入的样式<head>比你在问题中列出的更长/更大我建议使用@erenon关于动态样式表的建议以及我刚才提到的关于修改我的函数和钩子的内容样式表而不是打印样式.