2013-02-10 230 views
26

我花了很多時間弄清楚爲什麼我的搜索不能在我的自定義模板中工作。到目前爲止,我已經能夠弄清楚如何在我的頭文件中包含searchform.php文件,創建了當前爲空的search.php文件(因此,當我搜索某些東西時,我將其重定向到一個空白頁面,並且我認爲我確定需要在search.php文件中做一些工作),我正在閱讀Wordpress codex的所有內容,但找不到解決方案,只有我發現的有用信息就是這樣。如何顯示Wordpress搜索結果?

http://codex.wordpress.org/Creating_a_Search_Page

您能否提供以顯示搜索結果做什麼需要的?有沒有特殊的查詢,功能等?我真的無法在任何地方找到它。

我的searchform.php文件,以防您需要它。

<form action="<?php echo home_url(); ?>" id="search-form" method="get"> 
    <input type="text" name="s" id="s" value="type your search" onblur="if(this.value=='')this.value='type your search'" 
    onfocus="if(this.value=='type your search')this.value=''" /> 
    <input type="hidden" value="submit" /> 
</form> 
+0

是不是這樣[這個其他問題?](http://stackoverflow.com/questions/14800675/wordpress-not-displaying-search-results) – 2013-02-10 20:58:53

+0

@DamienPirsy這是,我刪除了上一個問題因爲結果讓人難以理解,所以我簡化了它。 – Ilja 2013-02-10 21:02:24

回答

16

基本上,您需要在您的search.php模板中包含Wordpress循環來遍歷搜索結果並將它們顯示爲模板的一部分。

下面是在ThemeShaper上從The WordPress Theme Search Template and Page Template開始的一個非常基本的例子。

<?php 
/** 
* The template for displaying Search Results pages. 
* 
* @package Shape 
* @since Shape 1.0 
*/ 

get_header(); ?> 

     <section id="primary" class="content-area"> 
      <div id="content" class="site-content" role="main"> 

      <?php if (have_posts()) : ?> 

       <header class="page-header"> 
        <h1 class="page-title"><?php printf(__('Search Results for: %s', 'shape'), '<span>' . get_search_query() . '</span>'); ?></h1> 
       </header><!-- .page-header --> 

       <?php shape_content_nav('nav-above'); ?> 

       <?php /* Start the Loop */ ?> 
       <?php while (have_posts()) : the_post(); ?> 

        <?php get_template_part('content', 'search'); ?> 

       <?php endwhile; ?> 

       <?php shape_content_nav('nav-below'); ?> 

      <?php else : ?> 

       <?php get_template_part('no-results', 'search'); ?> 

      <?php endif; ?> 

      </div><!-- #content .site-content --> 
     </section><!-- #primary .content-area --> 

<?php get_sidebar(); ?> 
<?php get_footer(); ?> 
4

檢查你的theme文件夾模板是否包含search.phpsearchform.php與否。

9

我正在使用searchform.phpsearch.php文件,但在此我提供了實際的代碼。

Creating a Search Pagecodex頁面幫助這裏和#Creating_a_Search_Page_Template顯示搜索查詢。

在我的情況下,我通過$search_query參數WP_QueryClass(它可以確定是否搜索查詢!)。然後我運行The Loop來顯示我想要的發佈信息,在我的情況下是the_permalinkthe_title

搜索框形式:

<form class="search" method="get" action="<?php echo home_url(); ?>" role="search"> 
    <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') ?>" /> 
    <button type="submit" role="button" class="btn btn-default right"/><span class="glyphicon glyphicon-search white"></span></button> 
</form> 

search.php模板文件:

<?php 
    global $query_string; 
    $query_args = explode("&", $query_string); 
    $search_query = array(); 

    foreach($query_args as $key => $string) { 
     $query_split = explode("=", $string); 
     $search_query[$query_split[0]] = urldecode($query_split[1]); 
    } // foreach 

    $the_query = new WP_Query($search_query); 
    if ($the_query->have_posts()) : 
    ?> 
    <!-- the loop --> 

    <ul>  
    <?php while ($the_query->have_posts()) : $the_query->the_post(); ?> 
     <li> 
      <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
     </li> 
    <?php endwhile; ?> 
    </ul> 
    <!-- end of the loop --> 

    <?php wp_reset_postdata(); ?> 

<?php else : ?> 
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> 
<?php endif; ?> 
21

您需要在您的search.php WordPress的循環 這個例子

的search.php模板文件:

<?php get_header(); ?> 
<?php 
$s=get_search_query(); 
$args = array(
       's' =>$s 
      ); 
    // The Query 
$the_query = new WP_Query($args); 
if ($the_query->have_posts()) { 
     _e("<h2 style='font-weight:bold;color:#000'>Search Results for: ".get_query_var('s')."</h2>"); 
     while ($the_query->have_posts()) { 
      $the_query->the_post(); 
       ?> 
        <li> 
         <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
        </li> 
       <?php 
     } 
    }else{ 
?> 
     <h2 style='font-weight:bold;color:#000'>Nothing Found</h2> 
     <div class="alert alert-info"> 
      <p>Sorry, but nothing matched your search criteria. Please try again with some different keywords.</p> 
     </div> 
<?php } ?> 

<?php get_sidebar(); ?> 
<?php get_footer(); ?> 
+0

謝謝。這個對我有用。我也想爲搜索結果計數。找到4個搜索結果。任何想法如何? – pagol 2015-05-11 09:43:44

+0

新代碼像圖片添加新變種: 這是圖片:http://i.imgur.com/8NgI15D.png – 2015-05-12 11:10:43

+0

要檢查結果計數使用此:<?全球$ wp_query; $ total_results = $ wp_query-> found_posts; ?>。文檔:https://codex.wordpress.org/Creating_a_Search_Page – 2016-03-16 16:31:23