【发布时间】:2012-12-03 23:26:44
【问题描述】:
我正在创建一个 wordpress 模板,我想在其中集成一个简单的搜索表单。在过去的 3 天里,我在 Google 上搜索了很多关于此的内容并尝试了几个教程,但恐怕有些东西我根本没有得到。我有 3 页可供搜索:search.php、searchform.php 和 searchpage.php。我阅读的所有教程都提供了类似的代码:
搜索.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,但我不知道如何改变它。感谢您的建议和帮助,非常感谢!
【问题讨论】:
-
此外,还有许多插件,也可以为这 3 个页面使用 Google 搜索 API 之一,以及全局搜索。真的很容易安装。