【问题标题】:Basic search in wordpresswordpress 中的基本搜索
【发布时间】:2012-12-03 23:26:44
【问题描述】:

我正在创建一个 wordpress 模板,我想在其中集成一个简单的搜索表单。在过去的 3 天里,我在 Google 上搜索了很多关于此的内容并尝试了几个教程,但恐怕有些东西我根本没有得到。我有 3 页可供搜索:search.phpsearchform.phpsearchpage.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 之一,以及全局搜索。真的很容易安装。

标签: wordpress search


【解决方案1】:

重要的文件是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(); ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 1970-01-01
    • 2014-06-11
    • 2018-11-10
    • 2010-11-24
    相关资源
    最近更新 更多