English 中文(简体)
如何从functions.php将以下css代码添加到我的页眉?
原标题:How can I add the following css code to my page header from functions.php?

现在,我将以下代码放在<code>header.php</code>中。

我认为这个解决方案不是很优雅。

如何将functions.php中的CSS代码添加到我的头中(该代码看起来如何)?

    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>
最佳回答

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

函数.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 );

假设您的主题构建正确并且具有<code>wp_head()<;头部>函数之外的任何文件进行修改。php


我要补充的是,由于外部样式表的客户端缓存,为了优化网站负载和增强性能,你应该制作一个单独的样式表,然后不是我上面提到的打印CSS的功能,而是打印<;链接>添加到样式表中。

而您可以不使用<code><;?php-bloginfo(template_directory)>,而是使用相对url(即../images/…..),这仍然会对get_option(THEME_PREFIX.intro_image)造成问题,所以如果你的样式表更改真的这么小,我在上面使用钩子列出的是好的解决方案,如果你想注入<;头部>比我建议使用@erenon关于动态样式表的建议和我刚才提到的关于修改我的函数&;挂钩以包含样式表,而不是打印样式。

问题回答

您可以很容易地将CSS封装在函数中

function headerCSS(){
    ?>
      // YOUR CSS
    <?php
}

然后调用函数

<HEAD>
<?php headerCSS(); ?>
//other header stuff here
<?HEAD>
<BODY>

您不能调用,但可以将其插入为css:

<link href="generate-css.php" type="text/css" rel="stylesheet" />

@陈:假设你的目录结构应该是这样的

/wp-content/
  /themes/
    /your_theme/
      /images/

把所有这些样式都放在没有<;?php-bloginfo(template_directory)>/wp-content/themes/your_theme/内的styles.css文件中的code>,然后在header.php中只需

<link rel="stylesheet" href="<?php bloginfo( template_directory ); ?>/styles.css" media="screen" type="text/css" />

这是一个糟糕的解决方案。外部样式表由服务器缓存。您正在使用的方法意味着它们必须在每个页面中提供,并且在服务器端更耗费资源。

请考虑使用标记来设置站点资源路径。

The base tag is very useful for Web designers who build websites in one location but will ultimately be placed in another location.

请参阅:http://webdesign.about.com/od/htmltags/qt/how-to-use-html-base-tag.htm





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

Drop down background url in Safari Issue

selectBox.selectCSS { background: url(/Images/replacementSelectBackground.png) top left no-repeat height:auto; } I have an issue in Safari only where the image is not rendering on top ...

CSS specific for Safari

How do you target specifically safari in css with styles?

Aligning textarea in a form

Ive used the following css code to align my form elements: form { position:relative; } form input { position:absolute; left:11em; } However, the textarea element is not aligned correctly with the ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

CSS problem with page footer

I have defined my page footer in the css file as: #footer { position: absolute; height: 50px; text-align: center; background: #66CCCC; bottom: 0px; left: 0px; width: 100%; height: ...

热门标签