我在this网站上的搜索表单有什么问题? 当我搜索某些内容时,它会显示所有帖子,而不仅仅是满足搜索条件的帖子。
这是搜索表单代码:
<?php $search_text = "search"; ?>
<form method="get" id="search-bg" action="<?php bloginfo('home'); ?>/">
<input type="text" value="<?php echo $search_text; ?>"
name="st" id="search-form"
onblur="javascript: if( this.value != 'search') { this.value = 'search'; this.style.color = '#888888'; }"
onclick="javascript: if( this.value == 'search') { this.value = ''; this.style.color = '#000000'; }"
)
{this.value = '';}" />
<input type="hidden" id="searchsubmit" />
</form>
另外,如果我将上面的代码复制到search-form.php
并将其替换为<?php search-form(); ?>
中的<?php get_search_form( $echo ); ?>
或sidebar.php
,它的功能是否相同?
答案 0 :(得分:1)
尝试使用默认的id值。我不知道这是不是你的问题,但可能是:
<?php $search_text = "search"; ?>
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<input id="s" type="text" value="<?php echo $search_text; ?>"
name="st" id="search-form"
onblur="javascript: if( this.value != 'search') { this.value = 'search'; this.style.color = '#888888'; }"
onclick="javascript: if( this.value == 'search') { this.value = ''; this.style.color = '#000000'; }"
)
{this.value = '';}" />
<input type="hidden" id="searchsubmit" />
</form>
答案 1 :(得分:0)
好的,这是错的:
因为我刚刚将我的主页模板复制到search.php
,所以它中包含以下代码:
<?php query_posts( array(
'posts_per_page' => 16,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 )).'&order=ASC');?>
这就是为什么搜索不起作用的原因。我删除后,它完美无缺。
所以这些是我的最终代码:
sidebar.php
:
<aside>
<a href="http://facebook.com/username" id="facebook"></a>
<a href="http://twitter.com/IndiaTheFall" id="twitter"></a>
<?php get_search_form( $echo ); ?>
<div id="categories">
<div id="categories-title"><h6><a href="/categories">Categories</a></h6></div>
<ul><h11><?php wp_list_categories('title_li='); ?></h11></ul>
</div>
<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Widgets')) : else : ?>
<!-- All this stuff in here only shows up if you DON'T have any widgets active in this zone -->
<?php endif; ?>
</aside>
searchform.php
:
<?php $search_text = "search"; ?>
<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<input type="text" value="<?php echo $search_text; ?>"
name="s" id="search-form"
onblur="javascript: if( this.value != 'search') { this.value = 'search'; this.style.color = '#888888'; }"
onclick="javascript: if( this.value == 'search') { this.value = ''; this.style.color = '#000000'; }"
)
{this.value = '';}" />
<input type="hidden" id="searchsubmit" />
</form>
search.php
:
<?php get_header(); ?>
<nav>
<?php wp_nav_menu(array('menu' => 'Main Nav Menu')); ?>
</nav>
<div id="main-content-archive">
<h85 class="inner_text_shadow"><?php printf( __( 'Search Results for: %s' ), '<span>' . get_search_query() . '</span>' ); ?></h85>
<div id="clear-box">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php $bg_image = post_thumb( get_the_post_thumbnail() );?>
<a href="<?php the_permalink() ?>"><div class="post-bg" style="background: #777777 url(<?php echo $bg_image;?>);">
<div id="title-bg">
<div class="transparency"></div>
<div class="title"><h10><?php the_title(); ?></h10></div>
</div>
</div></a> <!-- END post-bg -->
</div>
<?php endwhile; ?>
<?php else : ?>
<h2>No posts meet your criteria.</h2>
<?php endif; ?>
</div> <!-- END post_class function -->
<div id="nav"><h12><?php include (TEMPLATEPATH . '/inc/nav.php' ); ?></h12></div>
</div> <!-- END main-content -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
就是这样......现在一切正常! 谢谢你的帮助......