2013-11-03 629 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以获得解释)。

相关问题