【问题标题】:Customize search results for Custom Post Types自定义自定义帖子类型的搜索结果
【发布时间】:2013-11-03 17:39:56
【问题描述】:

我正在编写一个 Wordpress 插件,它可以创建多个自定义帖子类型 (CPT)。因为他们有自己的自定义字段,需要显示在搜索结果中,我需要自定义搜索结果输出。

我是否需要为此编写自己的主题,或者是否有一个钩子(或其他方式)可以在我的插件代码中解决这个问题?

【问题讨论】:

    标签: wordpress custom-post-type custom-fields


    【解决方案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;
    }
    

    【讨论】:

      【解决方案2】:

      我看到了四种可能性:

      1. 创建一个新的 (child) theme
      2. 使用filters (template_include) 覆盖搜索模板
      3. 使用客户端代码来修改外观(CSS / JavaScript,糟糕的解决方法)
      4. 挂钩the_contentthe_excerpt

      最简单的方法可能是复制您已安装主题的search.php file 并对其进行修改以满足您的需求。然后,您可以使用第一种或第二种方式将其挂钩。第一个需要你create a child theme,第二个需要创建一个插件。后者可能更复杂,所以我建议创建一个主题(查看template files of child themes 以获得解释)。

      【讨论】:

        猜你喜欢
        • 2014-01-03
        • 1970-01-01
        • 1970-01-01
        • 2017-11-22
        • 1970-01-01
        • 2013-06-20
        • 2011-10-23
        • 2023-01-31
        • 1970-01-01
        相关资源
        最近更新 更多