簡體   English   中英

wordpress中的add_action()函數不起作用

[英]add_action() function in wordpress not working

我是Wordpress的新手,我在我的functions.php編寫了這段代碼,但它無效。 這是我的代碼:

<?php

function myFunction(){
    wp_enqueue_style('style',get_stylesheet_uri());
}
add_action('wp_enqueue_scripts','myFunction');

此函數必須在我的頁面中添加我的style.css但不起作用。

請查看wp_enqueue_scripts的所有參數

試試以下代碼:

/**
 * Proper way to enqueue scripts and styles
 */
function myFunction() {
    wp_enqueue_style( 'style-name', get_stylesheet_uri() );
    wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'myFunction' );

試試這個:

add_action( 'after_setup_theme', 'yourtheme_theme_setup' );

if ( ! function_exists( 'yourtheme_theme_setup' ) ) {
    function yourtheme_theme_setup() {

        add_action( 'wp_enqueue_scripts', 'yourtheme_scripts' );

    }
}

if ( ! function_exists( 'yourtheme_scripts' ) ) {
    function yourtheme_scripts() {

        wp_enqueue_style( 'main_style', get_stylesheet_uri(), array() );

    }
}

這里wp_enqueue_scripts動作在after_setup_theme鈎子內after_setup_theme

https://developer.wordpress.org/reference/functions/wp_enqueue_style/

添加頭文件

<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo( 'stylesheet_url' ); ?>" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM