<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>wp_enqueue_script &#8211; 小豬日常</title>
	<atom:link href="https://piglife.tw/tag/wp_enqueue_script/feed/" rel="self" type="application/rss+xml" />
	<link>https://piglife.tw</link>
	<description>Hello World，一個紀錄生活與學習的地方</description>
	<lastBuildDate>Fri, 12 Dec 2025 14:47:12 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://piglife.tw/wp-content/uploads/2017/10/cropped-logo-1-32x32.png</url>
	<title>wp_enqueue_script &#8211; 小豬日常</title>
	<link>https://piglife.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>WordPress 自定義 CSS 與 JS 檔案載入實作筆記</title>
		<link>https://piglife.tw/technical-notes/wordpress-custom-css-js/</link>
					<comments>https://piglife.tw/technical-notes/wordpress-custom-css-js/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Sun, 18 Dec 2022 08:47:22 +0000</pubDate>
				<category><![CDATA[技術筆記]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[functions.php]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[wp_enqueue_script]]></category>
		<category><![CDATA[wp_enqueue_style]]></category>
		<guid isPermaLink="false">https://piglife.tw/?p=407</guid>

					<description><![CDATA[說明如何在 WordPress 主題的 functions.php 中載入自定義 CSS 和 JS ...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>在開發 WordPress 網站時，常常需要加入自定義的 CSS 和 JavaScript 來調整樣式或增加互動效果。直接在主題的 functions.php 檔案中註冊並載入這些檔案，是一種方便且易於維護的做法，適合本地開發與版本控管。</p>
<h2 class="wp-block-heading">在 functions.php 中載入自定義 CSS 和 JS</h2>
<p>WordPress 提供了 wp_enqueue_style 和 wp_enqueue_script 兩個函式，分別用來載入 CSS 和 JS 檔案。通常會將這些載入動作放在一個自訂函式中，並透過 add_action 掛載到 wp_enqueue_scripts 鉤子。</p>
<p>以下是範例程式碼片段：</p>
<pre><code class="lang-php language-php php">add_action( &#039;wp_enqueue_scripts&#039;, &#039;custom_style_scripts&#039; );

/**
 * Enqueue scripts and styles.
 */
function custom_style_scripts() {
    // CSS 檔案放在子主題的 css 資料夾中
    wp_enqueue_style( &#039;main_css&#039;, get_stylesheet_directory_uri() . &#039;/css/main.css&#039; );

    // JS 檔案放在子主題的 js 資料夾中，依賴 jQuery，並放置於頁面底部
    wp_enqueue_script( &#039;main_js&#039;, get_stylesheet_directory_uri() . &#039;/js/main.js&#039;, array(&#039;jquery&#039;), false, true );
}</code></pre>
<h2 class="wp-block-heading">實務應用與注意事項</h2>
<ul>
<li>建議將 CSS 和 JS 分別放在子主題的 css 和 js 資料夾中，方便管理與版本控制。</li>
<li>在本地開發時，可以搭配 Gulp 或其他前端工具編譯 SCSS，並自動更新這些檔案。</li>
<li>wp_enqueue_script 的最後一個參數設為 true，確保 JS 載入於頁面底部，提升頁面載入效能。</li>
<li>若有多個腳本或樣式檔案，依照需求分別註冊並載入，避免重複或衝突。</li>
</ul>
<h2 class="wp-block-heading">相關官方文件</h2>
<ul>
<li><a href="https://developer.wordpress.org/reference/functions/wp_enqueue_script/" target="_blank" rel="noopener">wp_enqueue_script</a></li>
<li><a href="https://developer.wordpress.org/reference/functions/wp_enqueue_style/" target="_blank" rel="noopener">wp_enqueue_style</a></li>
</ul>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code class="lang-text language-text text">add_action( &#039;wp_enqueue_scripts&#039;, &#039;custom_style_scripts&#039; );

/**
 * Enqueue scripts and styles.
 */
function custom_style_scripts() {
    //目錄可自行在子主題下面建立路徑像是css都放到css資料夾中，js就放到js資料夾中
    //載入CSS
    wp_enqueue_style( &#039;main_css&#039;, get_stylesheet_directory_uri() . &#039;/css/main.css&#039; );
    //載入JS
    wp_enqueue_script( &#039;main_js&#039;, get_stylesheet_directory_uri() . &#039;/js/main.js&#039;, array(&#039;jquery&#039;), false, true );
}</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/wordpress-custom-css-js/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
