【发布时间】:2014-12-20 23:39:21
【问题描述】:
很抱歉仍然问这个问题,这是在我尽力回答之前的答案之后。在 functions.php 中添加样式表切换器函数没有任何效果,因为 wp_head() 始终应用默认样式表(style.css)。
添加:
add_action( 'wp_enqueue_scripts', 'customstyles' );
function customstyles() { echo "hello world";
if ( is_page('306') ) {
wp_enqueue_style( '306page', get_stylesheet_uri()); //page-specific css for unique page
}
else {wp_enqueue_style( 'styles', get_stylesheet_uri() ); } //default stylesheet
}
在主题的functions.php中,然后从header.php调用<?php customstyles(); ?>没有效果,默认的style.css仍然应用到所有页面。
尝试从 header.php 中删除 wp_head() 并仅保留自定义函数,但随后没有应用样式表,这进一步证明 wp_head() 正在处理链接的 css。
那么如何自定义 wp_head() 使其不会覆盖通过 functions.php 或其他地方的代码添加的样式表?为什么首先忽略functions.php中的样式表代码?当在 customstyles() 中使用 echo 'hello world' 进行测试时,页面上会输出 hello world,但忽略了函数的样式表代码。 WP_DEBUG 也设置为 true 并且没有错误。
编辑:
这是head部分的wordpress代码,加上自定义函数调用:
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
<?php if (is_search()) { ?>
<meta name="robots" content="noindex, nofollow" />
<?php } ?>
<title>
<?php
if (function_exists('is_tag') && is_tag()) {
single_tag_title("Tag Archive for ""); echo '" - '; }
elseif (is_archive()) {
wp_title(''); echo ' Archive - '; }
elseif (is_search()) {
echo 'Search for "'.wp_specialchars($s).'" - '; }
elseif (!(is_404()) && (is_single()) || (is_page())) {
wp_title(''); echo ' - '; }
elseif (is_404()) {
echo 'Not Found - '; }
if (is_home()) {
bloginfo('name'); echo ' - '; bloginfo('description'); }
else {
bloginfo('name'); }
if ($paged>1) {
echo ' - page '. $paged; }
?>
</title>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
<?php customstyles(); ?>
<?php wp_head(); ?>
默认样式表实际上适用于应有的通用页面。它只是不应用“306”页上的自定义样式表。但是我已经验证了 wp_register_style( 'thisIsTheCustomStyleThatWontApplyButIsSpelledRightAndIsInTheSameDirectoryAsTheDefaultStylesheet', get_template_directory_uri() ) 方法中正确的样式表名称。在get_template_directory_uri()中加了“/”,没有解决。
【问题讨论】:
-
调用
get_stylesheet_uri时,WordPress会在返回的路径中添加style.css。 Codex 声明:样式表文件名为“style.css”,附加到 get_stylesheet_directory_uri() 路径。 codex.wordpress.org/Function_Reference/get_stylesheet_uri
标签: css wordpress customization stylesheet