dongtang4019 2019-01-23 00:47
浏览 49
已采纳

WordPress:如何为不同的页面排列两个样式表

EXPECTED OUTPUT

I am attempting to enqueue two stylesheets in Wordpress child theme.

I have written front-page.php for which I wish to apply exclusively front-page-style.css.

In addition, I have written header.php to override the parent theme's header. I have written style.css override the parent's stylesheet if it is in conflict.

ACTUAL OUTPUT

For neither front-page.php nor all other pages, neither the parent or the child stylesheet is being applied.

CODE

FOLDER STRUCTURE

+-- oceanwp
+-- oceanwp-child-theme-master
| +-- functions.php
| +-- style.css
| +-- front-page-style.css
| +-- front-page.php
| +-- header.php

Functions.php

function oceanwp_child_enqueue_parent_style() {
    // Dynamically get version number of the parent stylesheet (lets browsers re-cache your stylesheet when you update your theme)
    $theme   = wp_get_theme( 'OceanWP' );
    $version = $theme->get( 'Version' );
    // Load the stylesheet
    wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array( 'oceanwp-style' ), $version );

}

function my_theme_enqueue_styles() {
    $parent_style = 'oceanwp-style';
    // if the page is front-page.php, apply front-page-style.css
    if ( is_front_page() ) {
    wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' , '/style-rtl.css' );
        wp_enqueue_style( 'child-style',
            get_stylesheet_directory_uri() . '/front-page-style.css',
            array( $parent_style ),
            wp_get_theme('')->get('Version')
         );
    }

    // if the page is not front-page.php, apply style.css (CHILD) first and style.css (PARENT) second
    if (!is_front_page() ) {
        wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' , '/style-rtl.css' );
        wp_enqueue_style( 'child-style',
            get_stylesheet_directory_uri() . '/style.css',
            array( $parent_style ),
            wp_get_theme('')->get('Version')
         );
    }
}

add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles', 'my_custom_scripts' );

?>
  • 写回答

3条回答 默认 最新

  • dsarttv037029 2019-01-23 10:23
    关注

    You can use this function. Insert this code in functions.php

    add_action( 'wp_enqueue_scripts', 'styles_custom');
    function styles_custom() {
        global $post;
        $post_slug=$post->post_name;
    
        if ( is_front_page() ) { //only homepage
            wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' , '/style-home.css' );
        }
    
        if (!is_front_page() ) { //all page, not homepage
            wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' , '/style-all.css' );
        }
        if($post_slug == 'contact'){ //only page contact
            wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' , '/style-contact.css' );
        }
        if($post_slug == '[.....]'){
            wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' , '/style.css' );
        }
    }
    

    You can use post_slug, (e.g. Contact Us -> contact-us)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?