And*_*ili 0 css php wordpress wordpress-theming content-management-system
我正在尝试将CSS样式添加到我正在开发的WordPress主题中,如本教程所示:http://www.wpbeginner.com/wp-tutorials/how-to-properly-add-javascripts-and-styles -in-wordpress /但似乎不起作用,我不能理解为什么.
所以这是我个人主题的head.php文件:
<!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 />
Run Code Online (Sandbox Code Playgroud)
在我的functions.php文件中,我输入了以下代码:
<?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');
?>
Run Code Online (Sandbox Code Playgroud)
但是没有加载文件style.css.为什么?我错过了什么?
特别是在上一个教程中我无法理解的是如何调用wpb_adding_styles(),因为在header.php文件中它永远不会被调用.
有人可以帮帮我吗?
TNX
安德里亚
只需粘贴此代码:
<?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');
?>
Run Code Online (Sandbox Code Playgroud)
你的代码有问题:
plugins_url它实际打印http://example.com/wp-content/plugins
而不是
http://example.com/wp-content/themes/your-theme
在检索时使用get_stylesheet_uri
http://example.com/wp-content/themes/your-theme/style.css
不知道为什么本教程使用样式表的wp_register_script和wp_enqueue_script作为Javascript文件.使用wp_register_style和wp_enqueue_style作为css文件.