簡體   English   中英

wp_register_style()和wp_enqueue_style之間的區別

[英]Difference between wp_register_style() and wp_enqueue_style

我是WordPress開發的新手。 在查看一些示例代碼時,我遇到了wp_register_style() ,用於注冊樣式表及其位置,以后可以使用wp_enqueue_style()進行調用。

但是遍歷wp_enqueue_style()的文檔時,它說“如果提供了源(不覆蓋)並wp_enqueue_style() ,則注冊樣式”。

所以我的問題是兩種技術有什么區別。 直接使用wp_enqueue_style()而不是注冊,然后使用wp_register_style()wp_enqueue_style()調用是否正確? 有什么我想念的嗎?

這意味着,如果要注冊腳本,但不直接將其加載到頁面中,則可以注冊一次文件,然后在需要時加載它們。

例如:

您有一個switch語句,該語句加載了一些功能,但是三種情況中的兩種需要一個特定的javascript文件,而另一種則不需要。 您可以每次使腳本入隊,這會占用更多資源,也可以僅在需要時將其入隊:

...
wp_register_script( 'my-handy-javascript', ... );
...
switch( $somevar ) {
    case 'value':
        wp_enqueue_script( 'my-handy-javascript' ); // needs the file
        ...
    break;
    case 'value2':
        wp_enqueue_script( 'my-handy-javascript' ); // needs the file
        ...
    break;
    default:
    case 'value3': // doesn't needs the file
        ...
    break;
}

不必先注冊腳本然后將其排隊,但是如果您將所有需要的腳本注冊在functions.php中某個位置而不是代碼中的任何地方,則可以在代碼中提供一些邏輯。

食品法典還告知以下內容:

Use the wp_enqueue_scripts action to call this function, or admin_enqueue_scripts to call it on the admin side.

這意味着,如果要在前端和后端排隊腳本,可以注冊一次腳本,然后使用wp_enqueue_script將其加載到前端,然后使用admin_enqueue_script加載到后端。 這樣,您就不會在一個主題,插件,小部件或其他任何東西上兩次獲得相同的排隊資源。

暫無
暫無

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

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