ฉันมีช่องค้นหาสำหรับโพสต์บล็อก แต่ฉันต้องการฟิลด์อื่นสำหรับประเภทโพสต์ที่กำหนดเอง ฉันสามารถสร้างฟอร์มการค้นหาที่กำหนดเองที่มีความแตกต่างกันรูปแบบของผลการค้นหา ?
ฉันมีช่องค้นหาสำหรับโพสต์บล็อก แต่ฉันต้องการฟิลด์อื่นสำหรับประเภทโพสต์ที่กำหนดเอง ฉันสามารถสร้างฟอร์มการค้นหาที่กำหนดเองที่มีความแตกต่างกันรูปแบบของผลการค้นหา ?
คำตอบ:
นี่คือสิ่งที่ฉันได้ลองและได้วิธีแก้ปัญหาด้วย 3 ขั้นตอน สมมติว่าประเภทโพสต์ที่คุณกำหนดเองคือ " ผลิตภัณฑ์ "
1. เพิ่มรหัสฟังก์ชั่นที่นี่คุณสามารถระบุไฟล์เก็บถาวร - search.php
function template_chooser($template)   
{    
  global $wp_query;   
  $post_type = get_query_var('post_type');   
  if( $wp_query->is_search && $post_type == 'products' )   
  {
    return locate_template('archive-search.php');  //  redirect to archive-search.php
  }   
  return $template;   
}
add_filter('template_include', 'template_chooser');    
2. สร้างเทมเพลตผลการค้นหาสำหรับประเภทโพสต์ที่กำหนดเอง (archive-search.php)
        <?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><!-- #post -->    
                </div>
        <?php endwhile; ?>
    <?php endif; ?>
           </div><!-- content -->    
        </div><!-- contentarea -->   
        <?php get_sidebar(); ?>
        <?php get_footer(); ?>
สร้างแบบฟอร์มการค้นหา
 
ในแบบฟอร์มการค้นหานี้ค่า "ผลิตภัณฑ์" จะถูกซ่อนและจะค้นหาเฉพาะโพสต์ผลิตภัณฑ์
 <div>   
    <h3>Search Products</h3>
    <form role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">
    <input type="text" name="s" placeholder="Search Products"/>
    <input type="hidden" name="post_type" value="products" /> <!-- // hidden 'products' value -->
    <input type="submit" alt="Search" value="Search" />
  </form>
 </div>
มากฉันต้องการเชื่อมโยงคุณไปที่นี่
http://www.wpbeginner.com/wp-tutorials/how-to-create-advanced-search-form-in-wordpress-for-custom-post-types/
get_query_var('post_type')คืนค่าอาร์เรย์ (แทนที่จะเป็นสตริง) ดังนั้นจึงไม่สามารถเปรียบเทียบได้โดยตรง ตั้งแต่ฉันก็แค่ค้นหาประเภทหนึ่งโพสต์ในช่วงเวลาที่ผมก็เปลี่ยนของฉัน$post_typevar $post_type[0]ไป
                    http://localhost:3000/?s=cloud%27&post_type=productเป็นhttp://localhost:3000/search/cloud/product
                    search_templateกรองน่าจะเป็นทางเลือกที่เหมาะสมมากขึ้นเพื่อtemplate_include
                    นี่คือสิ่งที่ได้ผลสำหรับฉัน ไม่สะอาด แต่ฉันไม่สามารถรับคำตอบอื่น ๆ เหล่านี้ได้
ค้นหาฟอร์มสำหรับประเภทโพสต์ที่กำหนดเอง:
<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
    <label>
        <span class="screen-reader-text"><?php echo _x( 'Search for:', 'label' ) ?></span>
        <input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder' ) ?>" value="<?php echo get_search_query() ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" />
        <input type="hidden" name="post_type" value="book" />
    </label>
    <input type="submit" class="search-submit" value="<?php echo esc_attr_x( 'Search', 'submit button' ) ?>" />
</form>
ใน functions.php:
function searchfilter($query) {
    if ($query->is_search && !is_admin() ) {
        if(isset($_GET['post_type'])) {
            $type = $_GET['post_type'];
                if($type == 'book') {
                    $query->set('post_type',array('book'));
                }
        }       
    }
return $query;
}
add_filter('pre_get_posts','searchfilter');
ใน search.php:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
    <?php if(isset($_GET['post_type'])) {
        $type = $_GET['post_type'];
           if($type == 'book') {?>
               /* Format for "book" custom post type */
           <?php } else { ?>
               /* Format for custom post types that are not "book,"
               or you can use elseif to specify a second post type the
               same way as above. Copy the default format here if you
               only have one custom post type. */
           <?php } ?>
    <?php } else { ?>
              /* Format to display when the post_type parameter
              is not set (i.e. default format) */
<?php } ?>
<?php endwhile; else: ?>
/* What to display if there are no results. */
<?php endif; ?>
โดยปกติในทั้งสามแห่งคุณจะต้องแทนที่ "หนังสือ" ด้วยประเภทโพสต์ที่กำหนดเองของคุณ
หวังว่านี่จะช่วยใครซักคน!
รหัสสั้นเกิดขึ้นจริงมากขึ้น
 function template_chooser($template)   
{    
  global $wp_query; 
  $post_type = $wp_query->query_vars["pagename"];   
  if( isset($_GET['s']) && $post_type == 'products' )   
  {
    return locate_template('archive-search.php');  //  redirect to archive-search.php
  }   
  return $template;   
}
add_filter('template_include', 'template_chooser'); 
ฉันต้องการใช้สองรูปแบบที่แตกต่างกันสำหรับการค้นหาปกติของฉันและการค้นหาของฉันในประเภทโพสต์ที่กำหนดเอง
ประเภทโพสต์ที่กำหนดเองของฉันใช้ส่วนหัวที่แตกต่างจากหน้าปกติในหน้าปกติของฉันการโทรไปยังแบบฟอร์มการค้นหาของฉันคือ:
<?php get_search_form(true); ?>และการโทรไปยังแบบฟอร์มการค้นหาของฉันในส่วนหัวประเภทโพสต์ที่กำหนดเองคือ:
<?php get_template_part('search','library'); ?>ซึ่งมีฟิลด์เพิ่มเติม:
<input type="hidden" name="post_type" value="library" /> //Where "library" is my custom post type.ในไฟล์ฟังก์ชั่นฉันมีรหัสต่อไปนี้ที่คุณให้ไว้
/** Custom Search for Library */
function search_library($template)   
{    
  global $wp_query;   
  $post_type = get_query_var('post_type');   
  if( $wp_query->is_search && $post_type == 'library' )   
  {
    return locate_template('search-library.php');  //  redirect to archive-search.php
  }   
  return $template;   
}
add_filter('template_include', 'search_library');
ซึ่งจะตรวจสอบว่าฟอร์มการค้นหาทำการค้นหาภายในฟิลด์ที่กำหนดเองหรือไม่ดังนั้นจะแสดงการค้นหาในเทมเพลตที่กำหนดเองหรือใช้เทมเพลตปกติ
แก้ไข: แก้ไขการเรียกใช้ฟังก์ชั่น get_search_form () ซึ่งจะคืนค่าจริงไม่ว่าจะเกิดอะไรขึ้น
get_search_form('true') ที่กำลังมองหาการป้อนข้อมูลแบบบูลดังนั้นทั้งหรือ คุณกำลังป้อนสตริงไม่ใช่พารามิเตอร์บูลีน วิธีการตั้งค่าฟังก์ชั่นทั้งสองและจะส่งกลับผลลัพธ์เดียวกันเพราะพวกเขาทั้งสองสตริงที่ไม่ว่างเปล่า (ซึ่งทำให้ฟังก์ชั่นจะกลับจริงในทั้งสองกรณี) get_search_form(true)get_search_formtruefalse'true''false'
                    ในการแก้ไขปัญหาการค้นหาอินพุตว่างคุณสามารถแทนที่โค้ดฟังก์ชันด้วยสิ่งนี้:
function template_chooser($template)   
{    
 global $wp_query;   
 $post_type = get_query_var('post_type');   
 if( isset($_GET['s']) && $post_type == 'products' )   
 {
  return locate_template('archive-search.php');  //  redirect to archive-search.php
 }   
 return $template;   
}
add_filter('template_include', 'template_chooser');