本教程探討了WordPress主題開發中的關鍵header.php
文件。我們將檢查一個示例header.php
文件,詳細介紹必需品和非必需內容。
介紹
了解header.php
>的正確內容對於WordPress主題創建至關重要。 它不僅僅是徽標和菜單;它包括功能和SEO的基本要素。 其中包括:鏈接,元數據和各種標籤。
具體來說,header.php
應包含:
wp_enqueue_style
>函數functions.php
> 文件:更深的潛水
>
functions.php
>讓我們檢查
remove_action('wp_head', 'wp_generator'); function enqueue_styles() { wp_register_style( 'screen-style', get_template_directory_uri() . '/css_path/screen.css', array(), '1', 'all' ); wp_enqueue_style( 'screen-style' ); } add_action( 'wp_enqueue_scripts', 'enqueue_styles' ); function enqueue_scripts() { wp_register_script( 'html5-shim', 'https://html5shim.googlecode.com/svn/trunk/html5.js', array( 'jquery' ), '1', false ); wp_enqueue_script( 'html5-shim' ); wp_register_script( 'custom-script', get_template_directory_uri() . '/js_path/customscript.js', array( 'jquery' ), '1', false ); wp_enqueue_script( 'custom-script' ); } add_action( 'wp_enqueue_scripts', 'enqueue_scripts' );
>刪除元發電機標籤
> LINE remove_action('wp_head', 'wp_generator');
刪除了Meta Generator標籤,該標籤顯示WordPress版本。 這是一種防止潛在漏洞的安全最佳實踐。
> 和函數,掛在
>操作中,使用>和enqueue_styles()
(對於CSS)和enqueue_scripts()
>(對於JavaScript),以有效地管理樣式表和腳本。 這是將CSS和JS添加到WordPress主題中的推薦方法。 這些功能處理註冊和啟動資產,確保正確的加載順序並防止衝突。
wp_enqueue_scripts
wp_register_style
了解wp_enqueue_style
wp_register_script
filewp_enqueue_script
>文件應最小化:header.php
<meta charset="UTF-8">
<meta content="Keywords" name="keywords">
。 <meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport">
>
<title></title>
和wp_title()
用於動態標題生成的標籤。 bloginfo('name')
<div>包含標頭內容。 <ancy>>
結論<p><strong>
</strong>本指南為您的WordPress主題構建結構良好且安全的文件提供了基礎。 記住要利用</p>進行有效的資產管理。 <p>
<code>header.php
>wp_enqueue_scripts
這篇文章已通過Sajal Soni的貢獻進行了更新,Sajal Soni是印度的網站開發人員,專門從事開源框架。
以上是header.php文件:需要進行什麼,什麼都不是的詳細內容。更多資訊請關注PHP中文網其他相關文章!