WordPress只在文章页面加载指定JS/CSS

Kevin 杂记 1,377 次浏览 40条评论

对于某些特定功能JS或CSS文件,也许只是特定页面用到,不需要全站加载,这时可以按条件引入。话不多说,实现方法见下文(通过wp_enqueue_scripts函数加载文件,也是WordPress建议用法)

/**
 * Enqueue scripts and styles.
 */
function wp_load_scripts() {
    // 全站加载资源(演示文件引入方法)
    wp_enqueue_style( 'theme-style', get_theme_file_uri( '/assets/main.css' ), array(), null );
    wp_enqueue_script( 'theme-jquery', get_theme_file_uri( '/assets/main.js' ), array(), null );
    // 仅在“文章”和“页面”加载
    if ( is_single() || is_page() ) {
        wp_enqueue_script( 'post-jquery', get_theme_file_uri( '/assets/post.js' ), array(), null );
    }
    // 仅在指定“页面”加载(WP后台查看页面链接的ID参数)
    if ( is_page( ID ) ) {
        wp_enqueue_script( 'page-jquery', get_theme_file_uri( '/assets/page.js' ), array(), null );
    }
    // 仅在特定“页面模板”加载
    if( is_page() ) {
        global $wp_query;
        // 根据“页面模板”文件名匹配
        $page_template_name = get_post_meta( $wp_query->post->ID, '_wp_page_template', true );
        if($page_template_name == 'onecolumn-page.php'){
            wp_enqueue_script( 'page-jquery', get_theme_file_uri( '/assets/page.js' ), array(), null );		
        }
    }
}
add_action( 'wp_enqueue_scripts', 'wp_load_scripts' );

将代码放入functions.php文件生效。如有需要,你也可以按例子添加其它判断条件。

评论列表

  1. #1

    Keep up thee great piece of work, I read few osts on this website
    and I thin that your site is very interesting andd has got sets
    of wonderful information.

    Here is my web page :: Review Mfm5 Forex Robot

  2. #2

    It’s perfect tume to make a few plans for the long run and it’s time to be happy.
    I have read this put up and if I could I desire to suggest you few
    interesting things or advice. Maybe you can write subsequent articles relating to this article.
    I wis to read even more things about it!

    Also vist my web page :: mfm5 myfxbook

  3. #3

    normal dapoxetine dose https://salemeds24.wixsite.com/dapoxetine

  4. #4

    hydroxychloroquine for rheumatoid arthritis https://hydroxychloroquine.webbfenix.com/

  5. #5

    bimatoprost cvs https://carepro1st.com/

  6. #6

    liquid tadalafil for sale http://www.lm360.us/

  7. #7

    clomiphene success rate https://salemeds24.wixsite.com/clomid

  8. #8

    stromectol 3 mg price https://ivermectin.webbfenix.com/

  9. #9

    how to maximize vidalista effects https://vidalista.mlsmalta.com/

  10. #10

    hydroxychloroquine by state https://hydroxychloroquine.mlsmalta.com/

  11. #11

    how much is sildenafil http://droga5.net/

  12. #12

    ivermectin kills cancer https://ivermectin.mlsmalta.com/

  13. #13

    generic without a doctor generic dapoxetine https://dapoxetine.confrancisyalgomas.com/

  14. #14

    generic dapoxetine india buy online https://dapoxetine.bee-rich.com/

  15. #15

    is generic vidalista available https://vidalista40mg.mlsmalta.com/

  16. #16

    is cialis over counter update https://wisig.org/

  17. #17

    soft tabs dapoxetine https://ddapoxetine.com/

  18. #18

    amstyles.com https://amstyles.com/

  19. #19

    tadalafil to sildenafil conversion https://tadalafil.cleckleyfloors.com/

  20. #20

    hydroxychloroquine mexican pharmacy https://hydroxychloroquinee.com/

  21. #21

    hydroxychloroquine retinopathy symptoms https://sale.azhydroxychloroquine.com/

  22. #22

    hydroxychloroquine for covid https://hydroxychloroquine.grassfed.us/

  23. #23

    hydroxychloroquine for covid https://hydroxychloroquine.mymvrc.org/

  24. #24

    roman generic amoxicillin cost https://amoxycillin1st.com/

  25. #25

    hydroxychloroquine reviews side effects https://hhydroxychloroquine.com/

  26. #26

    what cancer does tamoxifen cause https://tamoxifen.mrdgeography.com/

  27. #27

    ivermectin 3mg tablets sale http://ivermmectin.co/

  28. #28

    cialis pills http://cialllis.com/

  29. #29

    generic daily cialis http://cialis.audiovideoninja.com/

  30. #30

    generic daily cialis https://cialis.mrdgeography.com/

  31. #31

    taking cialis and sildenafil together https://viaplz.com/

  32. #32

    Please let me know if you’re looking for a article author for your weblog.
    You have some really great articles and I think I would be a good
    asset. If you ever want to take some of the load off,
    I’d absolutely love to write some material for your blog in exchange for a link back
    to mine. Please blast me an e-mail if interested.
    Many thanks! http://antiibioticsland.com/Cipro.htm

  33. #33

    Thank you. I appreciate it. https://www.goldkamagra.com kamagra Ikwgn87

  34. #34

    viagra without doctor prescription

  35. #35

    tadalafil free trial online https://cialis.adicbc.com/

  36. #36

    tadalafil free trial online https://cialis.bee-rich.com/

  37. #37

    Well expressed truly! . https://www.withoutbro.com/# man dies overdose viagra Unaexwg

  38. #38

    I was recommended this website by my cousin. I am not
    sure whether this post is written by him as no one else
    know such detailed about my trouble. You are amazing!
    Thanks! http://herreramedical.org/prednisone

  39. #39

    ivermectin overdose human https://ivermectin1st.com/

  40. #40

    buy hydroxychloroquine online https://hydroxychloroquinex.com/

发表评论

电邮地址用于 Gravatar 头像显示,不会被公开可见。