doubi4814
2014-01-04 15:01
浏览 54
已采纳

Wordpress >>为不同的页面使用不同的CSS

I have having an issue with the following code :

<!--CSS files -->
<?php

function my_styles_method() {
    // Register the style like this for a theme:  
    wp_register_style('my-custom-style', get_template_directory_uri() . '/includes/front-page.css');
    // enqueue the stule  
    wp_enqueue_style('my-custom-style');
    // Register the style like this for a theme:  
    if (is_page_template('our-story.php')) {
        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');
?>

I am using the page template our-story.php and using the function as specified here :http://codex.wordpress.org/Conditional_Tags but somehow the main.css file doesn't get loaded. When I comment out the code for the front-page.css file and remove conditional statements than main.css files loaded and works correctly but not within this condition. I am not sure if I am using it correctly.

Looking forward to your response.

图片转代码服务由CSDN问答提供 功能建议

我遇到以下代码的问题:

   &lt;! -  CSS文件 - &gt; 
&lt;?php 
 
function my_styles_method(){
 //为主题注册这样的样式:
 wp_register_style('my-custom-style',get_template_directory_uri  ()。'/ includes / front-page.css'); 
 //将stule 
加入wp_enqueue_style('my-custom-style'); 
 //为主题注册这样的样式:
  if(is_page_template('our-story.php')){
 wp_register_style('my-custom-style',get_template_directory_uri()。'/ include / main.css'); .n //将stule列入
 wp_enqueue_style  ('my-custom-style'); 
} 
} 
 
add_action('wp_enqueue_scripts','my_styles_method'); 
?&gt; 
   
 
 <  p>我正在使用页面模板our-story.php并使用此处指定的函数: http:// codex。  wordpress.org/Conditional_Tags 但不知何故马 in.css文件没有加载。 当我注释掉front-page.css文件的代码并删除条件语句而不是main.css文件加载并正常工作但不在此条件下。 我不确定我是否正确使用它。   
 
 

期待您的回复。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dslk6326846 2014-01-04 15:13
    已采纳

    Why don't you use the body classes ? http://codex.wordpress.org/Function_Reference/body_class

    You can target whatever page / template you want with those classes. Ex:

    <body <?php body_class(); ?>>
    

    On the homepage will give you :

    <body class="home page page-id-14 page-template page-template-home-php ">
    

    So, if you want specific CSS for homepage :

    .home { Your styles }
    
    点赞 打赏 评论
  • dougaodi8895 2014-01-05 01:08

    I think may use this.

    if(is_page( 42 )) // id, title or slug name
    
    点赞 打赏 评论

相关推荐 更多相似问题