2013-11-03 289 views
0

我正在編寫一個創建多個自定義帖子類型(CPT)的Wordpress插件。因爲他們有自己的自定義字段,需要在搜索結果中顯示,所以我需要自定義搜索結果輸出。自定義自定義帖子類型的搜索結果

我是否需要編寫自己的主題,或者是否有掛鉤(或其他方式)在我的插件代碼中解決此問題?

回答

1

您可以勾選get_the_contentget_the_excerpt過濾器並使用is_search()進行測試,看看是否應該更改返回值。

沒有測試,但是這是想法:

add_filter('get_the_excerpt', 'my_search_excerpt'); 
add_filter('get_the_content', 'my_search_excerpt'); 

function my_search_excerpt($content) { 
    if (is_search()) 
     $content = 'This is a search excerpt for ' . get_the_title(); 
     // maybe add a read more link 
     // also, you can use global $post to access the current search result 
    } 
    return $content; 
} 
1

我看到四種可能性:

  1. 創建一個新的(childtheme
  2. 覆蓋使用filters搜索模板(template_include
  3. 使用客戶端代碼修改外觀(CSS/JavaScript,糟糕的workaroun d)
  4. 掛鉤the_contentthe_excerpt

最簡單的方法可能是複製已安裝的主題search.php file並修改它以滿足您的需求。然後,您可以使用第一種或第二種方式將其掛住。第一個需要你create a child theme,第二個創建一個插件。後者可能更復雜,所以我建議創建一個主題(請參閱template files of child themes以獲得解釋)。

相關問題