【问题标题】:Adding custom stylesheet for different WordPress pages为不同的 WordPress 页面添加自定义样式表
【发布时间】:2014-01-03 07:44:08
【问题描述】:

我创建了名为“front-page.php”的自定义主页,然后在header.php 文件中添加了另一个带有自定义 css 和 JS 文件的页面:

<?php 
function my_styles_method() {
        
    if (is_page_template('front-page.php'==TRUE){  
    // Register the style like this for a theme:  
    wp_register_style( 'my-custom-style', get_template_directory_uri().'/includes/marquee.css');  

    // enqueue the stule  
    wp_enqueue_style( 'my-custom-style' );  
    
    }
    

    enter code here
    // Register the style like this for a theme:  
    if (is_page_template('our-story.php'==TRUE) {
    wp_register_style( 'my-custom-style', get_template_directory_uri().'/includes/main.css');  

    // enqueue the stule  
    wp_enqueue_style( 'my-custom-style' );
    }
}  
add_action( 'wp_enqueue_scripts', 'my_styles_method' ); 

?>

现在,我面临以下问题: 当我在 WordPress 中运行它时,我收到以下错误消息:

解析错误:语法错误,意外的'{' in C:\wamp\www\wordpress_update\wp-content\themes\twentytwelve\header.php 在第 32 行

我在两个页面上都创建了自定义模板。

提前谢谢你。

【问题讨论】:

  • 第 32 行附近发生了什么?
  • 第 32 行是 if 语句 { 开始的地方..
  • so ... if (is_page_template('front-page.php'==TRUE){ 你能发现错误吗? PS:第二次也是如此……

标签: javascript php html css wordpress


【解决方案1】:

试试这个

<?php 
    function my_styles_method() {

        if (is_page_template('front-page.php')==TRUE){  
        // Register the style like this for a theme:  
        wp_register_style( 'my-custom-style', get_template_directory_uri().'/includes/marquee.css');  

        // enqueue the stule  
        wp_enqueue_style( 'my-custom-style' );  

        }


        //enter code here
        // Register the style like this for a theme:  
        if (is_page_template('our-story.php')==TRUE) {
        wp_register_style( 'my-custom-style', get_template_directory_uri().'/includes/main.css');  

        // enqueue the stule  
        wp_enqueue_style( 'my-custom-style' );
        }

    add_action( 'wp_enqueue_scripts', 'my_styles_method' ); 

    ?>

// if (is_page_template('front-page.php'==TRUE) 应该是 if (is_page_template('our-story.php')==TRUE)

【讨论】:

  • 是的,有问题 :) 谢谢你的帮助
  • 如果您想欣赏,请选择此答案作为正确答案
  • 我现在面临的问题是,当我这样做时,我的 css 文件没有被拾取。知道为什么会这样吗? css 在包含文件夹中,名为 main.css
  • 可能 get_template_directory_uri() 没有返回正确的路径。或者你期待的路径。您是否尝试过通过一些回声“调试”一些值和一些代码流?
猜你喜欢
  • 1970-01-01
  • 2011-12-19
  • 1970-01-01
  • 2017-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-14
  • 1970-01-01
相关资源
最近更新 更多