<?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>相關文章 &#8211; 小豬日常</title>
	<atom:link href="https://piglife.tw/tag/%e7%9b%b8%e9%97%9c%e6%96%87%e7%ab%a0/feed/" rel="self" type="application/rss+xml" />
	<link>https://piglife.tw</link>
	<description>Hello World，一個紀錄生活與學習的地方</description>
	<lastBuildDate>Fri, 12 Dec 2025 14:53:20 +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>相關文章 &#8211; 小豬日常</title>
	<link>https://piglife.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>WordPress 自定義文章類型相關文章顯示實作筆記</title>
		<link>https://piglife.tw/technical-notes/wordpress-related-posts-cpt/</link>
					<comments>https://piglife.tw/technical-notes/wordpress-related-posts-cpt/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Thu, 20 Feb 2025 07:18:16 +0000</pubDate>
				<category><![CDATA[技術筆記]]></category>
		<category><![CDATA[custom-post-type]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[wp_query]]></category>
		<category><![CDATA[相關文章]]></category>
		<category><![CDATA[自定義文章類型]]></category>
		<guid isPermaLink="false">https://piglife.tw/?p=495</guid>

					<description><![CDATA[介紹如何在 WordPress 自定義文章類型頁面中，利用 WP_Query 依據當前文章標題搜尋並...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>在 WordPress 開發中，常見需求是於自定義文章類型（Custom Post Type，CPT）頁面中顯示與該文章相關的一般文章。本文以名為 <code>member_info</code> 的 CPT 為例，示範如何利用 <code>WP_Query</code> 依據當前 CPT 文章標題搜尋並顯示相關文章。</p>
<h2 class="wp-block-heading">程式碼實現</h2>
<p>我們透過 <code>WP_Query</code> 搜尋標題或內容包含當前 <code>member_info</code> 文章標題的文章，並限制顯示數量。以下為範例程式碼，適合放入 <code>single-member_info.php</code> 或對應模板：</p>
<pre><code class="lang-php language-php php">if (is_singular(&#039;member_info&#039;)) {
  $current_title = get_the_title(); // 取得當前 member_info 文章標題

  $args = array(
    &#039;post_type&#039; =&gt; &#039;post&#039;, // 僅搜尋一般文章
    &#039;posts_per_page&#039; =&gt; 5, // 限制顯示數量
    &#039;s&#039; =&gt; $current_title, // 以標題作為搜尋關鍵字
  );

  $query = new WP_Query($args);

  if ($query-&gt;have_posts()) {
    echo &#039;
&lt;h3&gt;相關文章&lt;/h3&gt;&#039;;
    echo &#039;
&lt;ul&gt;&#039;;
    while ($query-&gt;have_posts()) {
      $query-&gt;the_post();
      echo &#039;
&lt;li&gt;&lt;a href=&quot;&#039; . get_permalink() . &#039;&quot;&gt;&#039; . get_the_title() . &#039;&lt;/a&gt;&lt;/li&gt;&#039;;
    }
    echo &#039;&lt;/ul&gt;&#039;;
  } else {
    echo &#039;
&lt;h3&gt;相關文章&lt;/h3&gt;&#039;;
    echo &#039;
&lt;p&gt;沒有找到相關文章&lt;/p&gt;&#039;;
  }

  wp_reset_postdata();
}</code></pre>
<h2 class="wp-block-heading">程式碼解析</h2>
<h3 class="wp-block-heading">1. 檢查文章類型</h3>
<pre><code class="lang-php language-php php">if (is_singular(&#039;member_info&#039;)) {</code></pre>
<p>確保程式碼只在 <code>member_info</code> 單篇文章頁執行，避免影響其他頁面。</p>
<h3 class="wp-block-heading">2. 取得當前文章標題</h3>
<pre><code class="lang-php language-php php">$current_title = get_the_title();</code></pre>
<p>利用 WordPress 內建函式取得目前 CPT 文章標題，作為搜尋關鍵字。</p>
<h3 class="wp-block-heading">3. 設定查詢參數</h3>
<pre><code class="lang-php language-php php">$args = array(
  &#039;post_type&#039; =&gt; &#039;post&#039;,
  &#039;posts_per_page&#039; =&gt; 5,
  &#039;s&#039; =&gt; $current_title,
);</code></pre>
<ul>
<li><code>post_type</code> 限定搜尋一般文章。</li>
<li><code>posts_per_page</code> 限制最多顯示 5 篇。</li>
<li><code>s</code> 參數用於全文搜尋，搜尋包含標題的文章。</li>
</ul>
<h3 class="wp-block-heading">4. 輸出查詢結果</h3>
<pre><code class="lang-php language-php php">if ($query-&gt;have_posts()) {
  echo &#039;
&lt;h3&gt;相關文章&lt;/h3&gt;&#039;;
  echo &#039;
&lt;ul&gt;&#039;;
  while ($query-&gt;have_posts()) {
    $query-&gt;the_post();
    echo &#039;
&lt;li&gt;&lt;a href=&quot;&#039; . get_permalink() . &#039;&quot;&gt;&#039; . get_the_title() . &#039;&lt;/a&gt;&lt;/li&gt;&#039;;
  }
  echo &#039;&lt;/ul&gt;&#039;;
} else {
  echo &#039;
&lt;h3&gt;相關文章&lt;/h3&gt;&#039;;
  echo &#039;
&lt;p&gt;沒有找到相關文章&lt;/p&gt;&#039;;
}</code></pre>
<p>判斷是否有符合條件的文章，並以列表形式輸出標題與連結。</p>
<h3 class="wp-block-heading">5. 重置查詢狀態</h3>
<pre><code class="lang-php language-php php">wp_reset_postdata();</code></pre>
<p>避免自訂查詢影響後續 WordPress 迴圈或內容顯示。</p>
<h2 class="wp-block-heading">實際應用與延伸</h2>
<ul>
<li>可用於產品頁面顯示與產品名稱相關的部落格文章。</li>
<li>課程頁面展示與課程名稱相關的學習資源。</li>
<li>可進一步結合自訂欄位或關聯插件，提升相關文章精準度。</li>
</ul>
<h2 class="wp-block-heading">常見坑</h2>
<ul>
<li><code>s</code> 參數為全文搜尋，可能導致結果不夠精確。</li>
<li>查詢結果過多時，建議加入分頁或限制數量。</li>
<li>請確保模板檔案命名與 CPT 名稱一致，避免無法正確載入。</li>
</ul>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code class="lang-text language-text text">&lt;?php
if (is_singular(&#039;member_info&#039;)) {
  $current_title = get_the_title(); // 取得當前 member_info 文章的標題

  // 查詢與當前 member_info 相關的 post 文章
  $args = array(
    &#039;post_type&#039; =&gt; &#039;post&#039;, // 只搜尋一般文章
    &#039;posts_per_page&#039; =&gt; 5, // 限制顯示數量
    &#039;s&#039; =&gt; $current_title, // 內文搜尋關鍵字
  );

  $query = new WP_Query($args);

  if ($query-&gt;have_posts()) {
    echo &#039;
&lt;h3&gt;相關文章&lt;/h3&gt;&#039;;
    echo &#039;
&lt;ul&gt;&#039;;
    while ($query-&gt;have_posts()) {
      $query-&gt;the_post();
      echo &#039;
&lt;li&gt;&lt;a href=&quot;&#039; . get_permalink() . &#039;&quot;&gt;&#039; . get_the_title() . &#039;&lt;/a&gt;&lt;/li&gt;&#039;;
    }
    echo &#039;&lt;/ul&gt;&#039;;
  } else {
    echo &#039;
&lt;h3&gt;相關文章&lt;/h3&gt;&#039;;
    echo &#039;
&lt;p&gt;沒有找到相關文章&lt;/p&gt;&#039;;
  }

  wp_reset_postdata();
}
?&gt;</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/wordpress-related-posts-cpt/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
