gpt4 book ai didi

php - 将子主题 css 添加到

转载 作者:太空宇宙 更新时间:2023-11-04 03:31:40 24 4
gpt4 key购买 nike

我有一个网站 ( http://lab.eksperimentar.com ),我正在使用 Delipress 主题,它是子主题。问题是:我在子主题中进行了更改,但页面未加载 styles.css。

我想将这个 html 元素“插入”到 head 标签中:

<link rel="stylesheet" id="delipress-child-style-css" href="http://lab.eksperimentar.com/wp-content/themes/delipress-child/style.css" type="text/css" media="all">

如果我打开子文件夹中的 function.php 并插入

<head>
<link rel="stylesheet" id="delipress-style-css" href="http://lab.eksperimentar.com/wp-content/themes/delipress/style.css?ver=2.5.1" type="text/css" media="all">
</head>

它破坏了我的网站,因为它没有“插入”head 标签,而是用它代替。

如何在不篡改父亲主题的情况下做到这一点?

最佳答案

正确的方法是使用 wp_register_style()函数,然后是 wp_enqueue_style() .

这两个函数都需要挂接到 wp_enqueue_script() 中.

来自 CODEX 的示例:

// Register style sheet.
add_action( 'wp_enqueue_scripts', 'register_plugin_styles' );

/**
* Register style sheet.
*/
function register_plugin_styles() {
wp_register_style( 'my-plugin', plugins_url( 'my-plugin/css/plugin.css' ) );
wp_enqueue_style( 'my-plugin' );
}

你也可以跳过寄存器,直接使用wp_enqueue_style()独自带着同一个钩子(Hook)

另请阅读 here使用子主题时..

add_action( 'wp_enqueue_scripts', 'load_my_child_styles', 20 );
function load_my_child_styles() {
wp_enqueue_style( 'child-theme', get_stylesheet_uri() );
}

另一个(不太推荐的)选项,如果你只有简单的 CSS 并且你想将它直接输出到 HEAD 将使用 wp_head() Action

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

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

</style>
<?php
wp_head
}

add_action('wp_head', 'add_styles'); ?>

请注意开始和结束 PHP 标签

关于php - 将子主题 css 添加到 <head>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26225702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com