【问题标题】:Can't change stylesheet in header.php无法更改 header.php 中的样式表
【发布时间】: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 &quot;"); echo '&quot; - '; }
              elseif (is_archive()) {
                 wp_title(''); echo ' Archive - '; }
              elseif (is_search()) {
                 echo 'Search for &quot;'.wp_specialchars($s).'&quot; - '; }
              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()中加了“/”,没有解决。

【问题讨论】:

标签: css wordpress customization stylesheet


【解决方案1】:
function customstyles()
{
    if ( is_page( '306' ) ) {

        //Register and enqueue the stylesheet for 306 page.
        wp_register_style( '306page', get_template_directory_uri() . '/css/306.css' );  
        wp_enqueue_style( '306page' );

    } else {

        //Register and enqueue the default stylesheet.    
        wp_register_style( 'styles', get_stylesheet_uri() );
        wp_enqueue_style( 'styles' );  

    }   
}
add_action( 'wp_enqueue_scripts', 'customstyles' );

上面的代码(未经测试)应该会有所帮助。

【讨论】:

  • 不幸的是,它没有任何区别。再次添加 echo 语句有效,但其余代码仍然对样式表没有影响。所以至少它不会是一个错误,因为 echo 语句有效并且没有错误消息,但也使得它更难弄清楚。
  • 请使用您尝试过的代码更新您的帖子,以便遇到此帖子的其他人可以相应地发表评论。我假设您的 header.php 中仍然有 wp_head()
  • 更新了我的帖子,默认样式表 'style.css' 会按预期应用于通用页面,而不是当前未应用样式表的唯一页面 '306' 上的自定义样式表。
  • 没人知道吗?最好在同一个线程中回答,从而防止我重新发布关于同一主题的另一个问题。我相信我已经清楚地解释了问题,如果不是,请告诉我。
  • 我检查了最后的代码,它工作正常。可能会尝试切换到默认主题之一,看看是否可行,只是为了解决这不是主题问题,而是更通用的问题。
猜你喜欢
  • 2014-08-13
  • 2017-02-23
  • 1970-01-01
  • 2021-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-11
相关资源
最近更新 更多