<?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>functions.php &#8211; 小豬日常</title>
	<atom:link href="https://piglife.tw/tag/functions-php/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>functions.php &#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>
		<item>
		<title>利用 Shortcode 在 WordPress 頁面顯示指定 Post Type 文章教學</title>
		<link>https://piglife.tw/technical-notes/wordpress-shortcode-post-type/</link>
					<comments>https://piglife.tw/technical-notes/wordpress-shortcode-post-type/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Wed, 01 Nov 2017 17:24:52 +0000</pubDate>
				<category><![CDATA[技術筆記]]></category>
		<category><![CDATA[functions.php]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[post type]]></category>
		<category><![CDATA[shortcode]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[wp_query]]></category>
		<category><![CDATA[自訂文章類型]]></category>
		<guid isPermaLink="false">https://piglife.tw/?p=265</guid>

					<description><![CDATA[介紹如何在 WordPress 中利用 shortcode 包裝 WP_Query 查詢指定文章類型...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>在使用 WordPress 建置網站時，若頁面內容多以 shortcode 方式組合，想要在頁面中靈活顯示特定文章類型（Post Type）的文章，就需要將查詢文章的功能包裝成 shortcode。這樣可以方便在任何頁面或文章中插入，提升內容管理彈性。本文適合有基礎 PHP 與 WordPress 開發經驗的工程師或自學者。</p>
<h2 class="wp-block-heading">建立自訂 Shortcode 函式</h2>
<p>我們可以在主題的 <code>functions.php</code> 檔案中，撰寫一個函式來查詢指定的文章類型，並輸出 HTML 結構。以下為範例關鍵程式碼說明：</p>
<pre><code class="lang-php language-php php">function home_post_listing_shortcode( $atts ) {
    ob_start();
    // 建立 WP_Query 物件，查詢 post_type 為 &#039;post&#039; 的文章，限制顯示 3 篇，依標題排序（降冪）
    $query = new WP_Query( array(
        &#039;post_type&#039;      =&gt; &#039;post&#039;, // 可替換成想抓取的自訂文章類型
        &#039;posts_per_page&#039; =&gt; 3,      // 取出文章數量
        &#039;order&#039;          =&gt; &#039;DESC&#039;, // 排序方式 ASC(小-&gt;大), DESC(大-&gt;小)
        &#039;orderby&#039;        =&gt; &#039;title&#039; // 依標題排序
    ) );

    if ( $query-&gt;have_posts() ) {
        echo &#039;&lt;ul class=&quot;info-listing&quot;&gt;&#039;;
        while ( $query-&gt;have_posts() ) {
            $query-&gt;the_post();
            ?&gt;
            &lt;li id=&quot;post-&lt;?php the_ID(); ?&gt;&quot; &lt;?php post_class(); ?&gt;&gt;
                &lt;div class=&quot;category_name&quot;&gt;&lt;?php the_category(); ?&gt;&lt;/div&gt;
                &lt;?php the_date(&#039;Y-m-d at g:ia&#039;, &#039;&lt;div class=&quot;news_date&quot;&gt;&#039;, &#039;&lt;/div&gt;&#039;); ?&gt;
                &lt;a href=&quot;&lt;?php the_permalink(); ?&gt;&quot;&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;
            &lt;/li&gt;
            &lt;?php
        }
        echo &#039;&lt;/ul&gt;&#039;;
        wp_reset_postdata();
    }

    return ob_get_clean();
}</code></pre>
<h2 class="wp-block-heading">註冊 Shortcode</h2>
<p>將上述函式註冊為 shortcode，讓 WordPress 辨識並可在頁面中使用：</p>
<pre><code class="lang-php language-php php">add_shortcode( &#039;list-posts-home&#039;, &#039;home_post_listing_shortcode&#039; );</code></pre>
<p>註冊後，只要在 WordPress 後台的頁面編輯器中插入 <code>[list-posts-home]</code>，就會顯示該 shortcode 所輸出的文章列表。</p>
<h2 class="wp-block-heading">實際應用與延伸</h2>
<ul>
<li>可修改 <code>post_type</code> 參數為自訂文章類型，如 <code>product</code>、<code>event</code> 等。</li>
<li>可加入 <code>category_name</code> 或 <code>tax_query</code> 參數，篩選特定分類文章。</li>
<li>調整 <code>posts_per_page</code> 與排序條件，符合不同需求。</li>
<li>搭配頁面建構器（如 Visual Composer）使用，提升內容排版彈性。</li>
</ul>
<h2 class="wp-block-heading">常見坑點</h2>
<ul>
<li><code>order</code> 參數應為 <code>DESC</code>（大到小），原範例中寫成 <code>DSC</code> 會無效。</li>
<li>使用 <code>the_date()</code> 時，若多篇文章同一天只會顯示一次日期，改用 <code>the_time()</code> 可避免此問題。</li>
<li>記得使用 <code>wp_reset_postdata()</code> 恢復全域文章資料，避免影響其他查詢。</li>
</ul>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code class="lang-text language-text text">function home_post_listing_shortcode( $atts ) {
    ob_start();
    $query = new WP_Query( array(
        &#039;post_type&#039;      =&gt; &#039;post&#039;,
        &#039;posts_per_page&#039; =&gt; 3,
        &#039;order&#039;          =&gt; &#039;DESC&#039;,
        &#039;orderby&#039;        =&gt; &#039;title&#039;,
    ) );

    if ( $query-&gt;have_posts() ) {
        echo &#039;&lt;ul class=&quot;info-listing&quot;&gt;&#039;;
        while ( $query-&gt;have_posts() ) {
            $query-&gt;the_post();
            ?&gt;
            &lt;li id=&quot;post-&lt;?php the_ID(); ?&gt;&quot; &lt;?php post_class(); ?&gt;&gt;
                &lt;div class=&quot;category_name&quot;&gt;&lt;?php the_category(); ?&gt;&lt;/div&gt;
                &lt;?php the_date(&#039;Y-m-d at g:ia&#039;, &#039;&lt;div class=&quot;news_date&quot;&gt;&#039;, &#039;&lt;/div&gt;&#039;); ?&gt;
                &lt;a href=&quot;&lt;?php the_permalink(); ?&gt;&quot;&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;
            &lt;/li&gt;
            &lt;?php
        }
        echo &#039;&lt;/ul&gt;&#039;;
        wp_reset_postdata();
    }

    return ob_get_clean();
}

add_shortcode( &#039;list-posts-home&#039;, &#039;home_post_listing_shortcode&#039; );</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/wordpress-shortcode-post-type/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
