首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我不能在这个WordPress主题中添加一个CSS样式?

为什么我不能在这个WordPress主题中添加一个CSS样式?
EN

Stack Overflow用户
提问于 2014-01-18 18:13:48
回答 3查看 510关注 0票数 0

我试图在我正在开发的WordPress主题中添加一个CSS样式,如本教程所示:http://www.wpbeginner.com/wp-tutorials/how-to-properly-add-javascripts-and-styles-in-wordpress/,但似乎不工作,我无法理解为什么。

这是我个人主题的head.php文件:

代码语言:javascript
复制
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
<title><?php bloginfo('name'); ?> <?php if ( is_single() ) { ?> » Blog Archive <?php } ?> <?php wp_title(); ?></title>
<meta name="generator" content="WordPress <?php bloginfo('version'); ?>" /> <!-- leave this for stats -->

<!--
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
<link rel="alternate" type="application/rss+xml" title="<?php bloginfo('name'); ?> RSS Feed" href="<?php bloginfo('rss2_url'); ?>" />
-->
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
<!-- <?php bloginfo('stylesheet_directory'); ?>/ -->
<script language="javascript" type="text/javascript" src="<?php bloginfo('stylesheet_directory'); ?>/js.js"></script>
<!--
<script language="javascript" type="text/javascript">    
    if(f)
    {
        write_css('<?php bloginfo('stylesheet_directory'); ?>');
    }
</script>
-->

<?php wp_head(); ?>
</head>
<body>
<center>
<div id="page">
<div id="header">

<h1><a href="<?php echo get_settings('home'); ?>"><?php bloginfo('name'); ?></a></h1>
<div class="description"><?php bloginfo('description'); ?></div>
</div>
<hr />

在我的functions.php文件中,我放置了以下代码:

代码语言:javascript
复制
<?php
function wpb_adding_styles() {
    wp_register_script('my_stylesheet', plugins_url('style.css', __FILE__));
    wp_enqueue_script('my_stylesheet');
}

add_action('wp_enqueue_scripts', 'wpb_adding_styles');
?>

但是没有加载文件style.css。为什么?我遗漏了什么?

特别是,在上一篇教程中我无法理解的是wpb_adding_styles()是如何被调用的,因为在header.php文件中从未调用过它。

有人能帮我吗?

Tnx

安德里亚

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-01-18 18:43:17

只需粘贴以下代码:

代码语言:javascript
复制
<?php
function wpb_adding_styles() {
    wp_register_style('my_stylesheet', get_stylesheet_uri());
    wp_enqueue_style('my_stylesheet');
}
add_action('wp_enqueue_scripts', 'wpb_adding_styles');
?>

代码中的问题:

url实际上是打印http://example.com/wp-content/plugins

而不是

http://example.com/wp-content/themes/your-theme

在检索uri时使用它

http://example.com/wp-content/themes/your-theme/style.css

不知道为什么教程使用脚本脚本作为样式表,两者都用于Javascript文件。对css文件使用风格风格

票数 5
EN

Stack Overflow用户

发布于 2014-01-18 18:42:28

对样式表(style.css)的引用是不正确的。plugins_url功能是为WP插件,但您正在尝试开发一个WP主题。

将此代码放入主题的functions.php文件中:

代码语言:javascript
复制
function wpb_adding_styles() {
     wp_register_script('my_stylesheet', get_stylesheet_uri() );
     wp_enqueue_script('my_stylesheet');
}

add_action('wp_enqueue_scripts', 'wpb_adding_styles');
票数 0
EN

Stack Overflow用户

发布于 2014-09-11 19:19:17

在您的functions.php中使用这个

代码语言:javascript
复制
/**
 * Proper way to enqueue scripts and styles
 */
function theme_name_scripts() {
    wp_enqueue_style( 'style-name', get_stylesheet_uri() );
    wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}

add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );

并将其添加到header.php

代码语言:javascript
复制
<?php wp_enqueue_scripts(); ?>

只需替换姓名和地址。如果您需要添加超过1个.css.js,只需复制并创建新行即可。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21207941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档