2012-12-03 45 views
1

我正在創建一個wordpress模板,我想在其中集成一個簡單的搜索表單。在過去的三天裏,我對此進行了大量搜索,並嘗試了幾個教程,但是恐怕有一些我根本得不到的東西。我有3頁用於搜索:search.phpsearchform.phpsearchpage.php。所有的教程我閱讀提供類似這樣的代碼:在wordpress中的基本搜索

的search.php:

<?php if (have_posts()) : ?> 
… 
<?php while (have_posts()) : the_post(); ?> 
… 
<?php endwhile; else: ?> 
… <p>The key word <strong><?php the_search_query(); ?></strong> is not on this website.</p> 
<?php include (TEMPLATEPATH . "/searchform.php"); ?> 
<?php include (TEMPLATEPATH . "/searchpage.php"); ?> 
<?php endif; ?> 

searchform.php:

<?php 
$querystring = esc_attr(apply_filters('the_search_query', get_search_query())); 
$searchstring = "Suchbegriff eingeben"; 
if (empty($querystring)) { $querystring = $searchstring; } 
?> 

<form method="get" id="searchform" action="<?php bloginfo('url'); ?>/"> 
<div> 
    <input type="text" name="s" id="s" value="<?php echo $querystring; ?>" 
     onblur="if (this.value == '') { this.value = '<?php echo $searchstring; ?>'; }" 
     onfocus="if (this.value == '<?php echo $searchstring; ?>') { this.value = ''; }" /> 
    <input type="submit" id="searchsubmit" value="Suchen" /> 
</div> 
</form> 

searchpage.php(改編自page.php文件):

<?php 
/* 
Template Name: Search Page 
*/ 
?> 

<div id="content"> 

<?php if (have_posts()) while (have_posts()) : the_post(); ?> 

    <div class="blogpost"> 
     <h2><?php the_title(); ?></h2> 
     <?php the_content(); ?> 
     <?php $this->posts = $wpdb->get_results($this->request); ?> 
    </div> <!-- end class blogpost --> 
<?php endwhile; ?> 

</div> 
<?php get_sidebar(); ?> 
<?php get_footer(); ?> 

如果沒有與關鍵字匹配的搜索工作。但是如果有一場比賽,我只能得到… … … … … … …作爲輸出。我知道必須從search.php,但我不知道如何改變這一點。感謝您的建議和幫助,我非常感謝!

+0

此外,有很多插件,也可以使用谷歌搜索API的這3頁,加上全球搜索。真的很容易安裝。 –

回答

2

重要的文件是search.php,它顯示任何匹配的結果,或者如果沒有匹配,則顯示失敗消息。嘗試這樣的:

<?php 
/** 
* Search results page 
*/ 
?> 
<?php if (have_posts()): ?> 
<h2>Search Results for '<?php echo get_search_query(); ?>'</h2> 
<ol> 
<?php while (have_posts()) : the_post(); ?> 
    <li> 
      <h2><a href="<?php esc_url(the_permalink()); ?>" title="Permalink to <?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></h2> 
      <time datetime="<?php the_time('Y-m-d'); ?>" pubdate><?php the_date(); ?> <?php the_time(); ?></time> <?php comments_popup_link('Leave a Comment', '1 Comment', '% Comments'); ?> 
      <?php the_content(); ?> 
    </li> 
<?php endwhile; ?> 
</ol> 
<?php else: ?> 
<h2>No results found for '<?php echo get_search_query(); ?>'</h2> 
<?php endif; ?> 

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

謝謝你,這很好! – Katy