我们为网站创建了一个新网址。旧的URL是blog.mysite.com。新网址是www.mysite.com/blog。我遇到的问题是,虽然css在blog.mysite.com上运行正常,但它无法在www.mysite.com/blog上运行。新网址正在www.mysite.com/wp-content上查找文件...而文件实际位于www.mysite.com/blog/wp-content ...新网址上(blog.mysite) .com / wp-content ...关于旧的)。两者都指向相同的服务器和相同的文件。这是一个例子:
<link href="/wp-content/themes/edition/prefix_css.css" rel="stylesheet">
<link href="/wp-content/themes/edition/header.css" rel="stylesheet">
答案 0 :(得分:1)
像@JonStirling一样,你应该更新你的href属性。它在blog.mysite.com上运行的原因是它是一个子域而不是新的url。
最好的方法是使用bloginfo()
动态添加域,这样您的代码就会变得类似:
<link href="<?php bloginfo('stylesheet_directory')?>/prefix_css.css" rel="stylesheet">
<link href="<?php bloginfo('stylesheet_directory')?>/header.css" rel="stylesheet">
通过这种方式,每次都可以正确加载文件的路径,如果它在子域上,则无关紧要。
答案 1 :(得分:1)
如果您使用的是Wordpress,建议使用WP提供的排队系统。如果你正确使用它,你可以用它做很多“技巧” - 用硬编码的样式表链接和脚本(几乎)是不可能的。
使用wp_head()功能 - 可以为您调用很多内容:例如enqueued stylesheets(或scripts)。
所以我认为您应该将代码更改为以下
// Put this code in your functions.php
function enqueue_stylesheets() {
// enqueueing prefix_css.css for all media, with a version number of 1.0.0
wp_enqueue_style(
'prefix_css',
get_stylesheet_directory_uri() . '/prefix_css.css',
array(),
'1.0.0',
'all'
);
// enqueueing header.css for all media, with a version number of 1.0.0; AFTER prefix_css.css
wp_enqueue_style(
'header',
get_stylesheet_directory_uri() . '/header.css',
array( 'prefix_css' ),
'1.0.0',
'all'
);
}
add_action( 'wp_enqueue_scripts', 'enqueue_stylesheets' );
从头部删除硬编码的样式表链接,然后更改标题:
<html>
<head>
<!-- everything that's in the header -->
<?php
wp_head();
?>
</head>
<!-- continuing with the <body> -->
这将输出您的样式表并尊重网站的网址结构。
当然,您可以使用bloginfo('stylesheet_directory'),但最佳做法是使用get_stylesheet_directory_uri()代替。