【发布时间】:2013-11-03 17:39:56
【问题描述】:
我正在编写一个 Wordpress 插件,它可以创建多个自定义帖子类型 (CPT)。因为他们有自己的自定义字段,需要显示在搜索结果中,我需要自定义搜索结果输出。
我是否需要为此编写自己的主题,或者是否有一个钩子(或其他方式)可以在我的插件代码中解决这个问题?
【问题讨论】:
标签: wordpress custom-post-type custom-fields
我正在编写一个 Wordpress 插件,它可以创建多个自定义帖子类型 (CPT)。因为他们有自己的自定义字段,需要显示在搜索结果中,我需要自定义搜索结果输出。
我是否需要为此编写自己的主题,或者是否有一个钩子(或其他方式)可以在我的插件代码中解决这个问题?
【问题讨论】:
标签: wordpress custom-post-type custom-fields
您可以连接到get_the_content 和get_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;
}
【讨论】:
我看到了四种可能性:
template_include) 覆盖搜索模板the_content 或the_excerpt
最简单的方法可能是复制您已安装主题的search.php file 并对其进行修改以满足您的需求。然后,您可以使用第一种或第二种方式将其挂钩。第一个需要你create a child theme,第二个需要创建一个插件。后者可能更复杂,所以我建议创建一个主题(查看template files of child themes 以获得解释)。
【讨论】: