<?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>shortcode &#8211; 小豬日常</title>
	<atom:link href="https://piglife.tw/tag/shortcode/feed/" rel="self" type="application/rss+xml" />
	<link>https://piglife.tw</link>
	<description>Hello World，一個紀錄生活與學習的地方</description>
	<lastBuildDate>Fri, 12 Dec 2025 14:45:27 +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>shortcode &#8211; 小豬日常</title>
	<link>https://piglife.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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>
