【问题标题】:Wordpress add search to custom post type archiveWordpress 将搜索添加到自定义帖子类型存档
【发布时间】:2016-05-19 00:10:58
【问题描述】:

我有一个包含许多自定义帖子类型的网站 例如playersteams ...

对于其中的每一个,我都有存档页面,例如:player-archive.php

在自定义帖子类型存档页面中,我想在顶部添加一个搜索(如果可能,进行过滤)。

PS:我看到其他问题的代码是关于搜索的,但它不起作用,没有答案,其他问题和插件,但它们通常可以改善网站中的所有搜索。

所以首先是否有可能对插件有任何建议也很棒。

【问题讨论】:

  • 到目前为止你做了什么?
  • 正如我告诉你的那样,我尝试了一些插件,但它们是通用的,并在我自己身上搜索了其他......另外,我发现有些人尝试了一些代码,但对他们不起作用,对我不起作用。 ...链接:stackoverflow.com/questions/26964261/…
  • 你还没有发送this

标签: php wordpress custom-post-type


【解决方案1】:

只需 3 步即可完成

假设您的自定义帖子类型是“玩家

1 . 添加功能代码
在这里你可以指定 archive-search.php

function template_chooser( $template ){
    global $wp_query;   
    $post_type = get_query_var('post_type');   
    if( $wp_query->is_search && $post_type == 'players' ){
        return locate_template('archive-search.php');  //  redirect to archive-search.php
    }   
    return $template;   
}
add_filter( 'template_include', 'template_chooser' );    

2 。为自定义帖子类型(search-archive.php)创建搜索结果模板

/* Template Name: Custom Search */        
get_header(); ?>             
<div class="contentarea">
    <div id="content" class="content_right">  
        <h3>Search Result for : <?php echo "$s"; ?> </h3>       
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>    
        <div id="post-<?php the_ID(); ?>" class="posts">        
            <article>        
                <h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a><h4>        
                <p><?php the_exerpt(); ?></p>        
                <p align="right"><a href="<?php the_permalink(); ?>">Read     More</a></p>
                <span class="post-meta"> Post By <?php the_author(); ?>    
                     | Date : <?php echo date('j F Y'); ?></span>    
            </article>
        </div>
        <?php endwhile; ?>
        <?php endif; ?>
    </div><!-- content -->
</div><!-- contentarea -->   
<?php get_sidebar(); ?>
<?php get_footer(); ?>

3 . 构建搜索表单
在此搜索表单中,“玩家”值是隐藏的,它只会搜索 玩家 帖子。

<div>   
    <h3>Search players</h3>
    <form role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">
        <input type="text" name="s" placeholder="Search players"/>
        <input type="hidden" name="post_type" value="players" /> <!-- // hidden 'players' value -->
        <input type="submit" alt="Search" value="Search" />
     </form>
 </div>

【讨论】:

  • Master's in copy paste and edit.甚至不认识作者的名字。
  • @jameshwartlopez 哈哈,反过来也是一样的。大约两三年前我在我的博客上发布了这个,顺便感谢分享链接:))
  • @Trix 我想​​你也应该认出作者的名字:D
【解决方案2】:

这 3 个步骤对我有用:

第 1 步:创建用于搜索的表单

第 2 步:在您的 function.php 中添加以下代码:

function searchfilter($query) {
    if ($query->is_search && !is_admin() ) {
        if(isset($_GET['post_type'])) {
            $type = $_GET['post_type'];
                if($type == 'product') {
                    $query->set('post_type',array('product'));
                }
        }       
    }
return $query;
}

add_filter('pre_get_posts','searchfilter');

第 3 步:创建一个 search.php 文件广告添加:

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
    <?php if(isset($_GET['post_type'])) {
        $type = $_GET['post_type'];
           if($type == 'product') {?>

               /* Format for "product" custom post type */

           <?php }?>
    <?php } else { ?>
<?php } ?>
<?php endwhile; else: ?>
<?php endif; ?>

供您参考:

http://www.wpbeginner.com/wp-tutorials/how-to-create-advanced-search-form-in-wordpress-for-custom-post-types/

http://wpsnipp.com/index.php/template/create-multiple-search-templates-for-custom-post-types/

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-19
    • 2013-08-04
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    • 2012-08-31
    相关资源
    最近更新 更多