2012-12-11 107 views
1

我已经研究过这个问题,但似乎无法弄清楚为什么我的搜索功能不适用于我正在处理的主题。下面是我的代码,我的search.php和我的searchform.php文件。搜索文件很难从样板,空白的WordPress主题修改。当我搜索任何东西时,它会返回一个404错误。我研究了搜索的返回404,仍然找不到解决方案。吹是我的代码,任何帮助将不胜感激。WordPress搜索问题

searchform.php ----------------------------------

<form action="<?php bloginfo('siteurl'); ?>" id="searchform" method="get"> 
     <label for="search"><object data='<?php bloginfo('template_directory');? >/images/input-search.svg'> 
     <img src='<?php bloginfo('template_directory');?>/images/input-search.png'> 
     </object></label> 
     <input type="search" name="search" /> 
</form> 

搜索。 PHP ---------------------------------------

<?php get_header(); ?> 

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

    <h2>Search Results</h2> 

    <?php include (TEMPLATEPATH . '/inc/nav.php'); ?> 

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

     <div <?php post_class() ?> id="post-<?php the_ID(); ?>"> 

      <h2><?php the_title(); ?></h2> 

      <?php include (TEMPLATEPATH . '/inc/meta.php'); ?> 

      <div class="entry"> 

       <?php the_excerpt(); ?> 

      </div> 

     </div> 

    <?php endwhile; ?> 

    <?php include (TEMPLATEPATH . '/inc/nav.php'); ?> 

<?php else : ?> 

    <h2>No posts found.</h2> 

<?php endif; ?> 

回答

0

您需要确保在您的functions.php文件中启用了搜索表单,如下所示:

// ENABLES SEARCH FORM STUFF 
function my_search_form($form) { 

$form = '<form role="search" method="get" id="searchform" action="' . home_url('/') . '" > 
<div><label class="screen-reader-text" for="s">' . __('Search for:') . '</label> 
<input type="text" value="' . get_search_query() . '" name="s" id="s" /> 
<input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" /> 
</div> 
</form>'; 

return $form; 

}

// ENABLES SEARCH FORM 
add_filter('get_search_form', 'my_search_form'); 

这是我在我的主题使用的代码。那么这将调用我的搜索形式:

<?php get_search_form(); ?> 

这是我searchform.php

<form method="get" id="searchform" action="<?php echo esc_url(home_url('/')); ?>"> 
    <label for="s" class="assistive-text"><?php _e('Search'); ?></label> 
    <input type="text" class="field" name="s" id="s" placeholder="<?php esc_attr_e('Search'); ?>" /> 
    <input type="submit" class="submit" name="submit" id="searchsubmit" value="<?php esc_attr_e('Search'); ?>" /> 
    </form> 
+0

好吧,我想通了,为什么它返回一个404 - 其实我有我的包裹在一个额外的表单标签中,所以在我的HTML以及我的searchform.php文件中都有一个表单标签。 但是,搜索结果现在将查询a ../?search=xxx,但它会返回数据库中的每个帖子。任何猜测为什么? –

+0

尝试删除动作=“<?php bloginfo('siteurl');?>”out of your searchform.php and puts action =“$ _ SERVER ['PHP_SELF']”instead ...让我知道这是否可行 –

+0

似乎没有这样做。这回去返回一个404。 –