美文网首页
wordpress修改css样式无效

wordpress修改css样式无效

作者: JairusTse | 来源:发表于2017-10-06 23:37 被阅读1062次

    在使用wordpress搭建网站过程中,需要自定义页面的模板,在style.css修改css样式,但是刷新页面并没有生效,在网上找原因,都是说缓存导致的,也有具体的解决方案,可能每个版本的处理有些出入,但是原理基本上是一致的,我的解决办法也记录下来:

    我用的是wordpress 4.8.2, lawyeria-lite主题,在 /wp-content/themes/lawyeria-lite/ 目录下找到 functions.php,然后在 functions.php 文件内搜索 wp_enqueue_style ,有几个地方调用了这个方法,其中 wp_enqueue_style( 'lawyeria_lite_style', get_stylesheet_uri(), array(), '1.0' ); 是要修改的地方:

    /**
     *  WP Enqueue Style
     */
    function lawyeria_lite_enqueue_style() {
        wp_enqueue_style( 'lawyeria_lite_style', get_stylesheet_uri(), array(), '1.0' );
        wp_enqueue_style( 'lawyeria_lite_fancybox', get_template_directory_uri() . '/css/jquery.fancybox.css', array(), '1.0' );
    }
    
    

    把1.0改成2.0或者更高,刷新页面css样式就可以立即生效了。

    上面的方法有个缺点,就是每次都要增加版本号,继续找更好的解决办法,找到了另外一个方法:在 functions.php 加上下面的代码,就可以每次都更新,使用这个方法上面的版本号也不需要改了。

    add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_styles', PHP_INT_MAX);
    function enqueue_child_theme_styles() {
        wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
        wp_enqueue_style( 'child-style', get_stylesheet_uri(), NULL, filemtime( get_stylesheet_directory() . '/style.css' ) );
    }
    
    

    相关文章

      网友评论

          本文标题:wordpress修改css样式无效

          本文链接:https://www.haomeiwen.com/subject/ldfdyxtx.html