Ilj*_*lja 33 php wordpress search
我花了很多时间弄清楚为什么我的搜索不能在我的自定义模板中工作.到目前为止,我能够弄清楚如何在我的标题中包含searchform.php文件,创建当前为空的search.php文件(所以当我搜索我被重定向到空白页面的那一刻时,我想我肯定在search.php文件中需要一些东西来使它工作),我正在阅读Wordpress的所有代码,但找不到解决方案,只有我找到的有用信息是这个.
http://codex.wordpress.org/Creating_a_Search_Page
您能否建议需要做些什么才能显示搜索结果?是否有特殊的查询,功能等?我真的找不到任何地方.
我的searchform.php文件,以防您需要它.
<form action="<?php echo home_url(); ?>" id="search-form" method="get">
<input type="text" name="s" id="s" value="type your search" onblur="if(this.value=='')this.value='type your search'"
onfocus="if(this.value=='type your search')this.value=''" />
<input type="hidden" value="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)
小智 33
你需要在search.php中包含Wordpress循环,这是一个例子
search.php模板文件:
<?php get_header(); ?>
<?php
$s=get_search_query();
$args = array(
's' =>$s
);
// The Query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
_e("<h2 style='font-weight:bold;color:#000'>Search Results for: ".get_query_var('s')."</h2>");
while ( $the_query->have_posts() ) {
$the_query->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php
}
}else{
?>
<h2 style='font-weight:bold;color:#000'>Nothing Found</h2>
<div class="alert alert-info">
<p>Sorry, but nothing matched your search criteria. Please try again with some different keywords.</p>
</div>
<?php } ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Run Code Online (Sandbox Code Playgroud)
Jam*_*den 21
基本上,您需要在search.php模板中包含Wordpress循环以循环搜索结果并将其显示为模板的一部分.
以下是来自ThemeShaper 的WordPress主题搜索模板和页面模板的一个非常基本的示例.
<?php
/**
* The template for displaying Search Results pages.
*
* @package Shape
* @since Shape 1.0
*/
get_header(); ?>
<section id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'shape' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
</header><!-- .page-header -->
<?php shape_content_nav( 'nav-above' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'search' ); ?>
<?php endwhile; ?>
<?php shape_content_nav( 'nav-below' ); ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'search' ); ?>
<?php endif; ?>
</div><!-- #content .site-content -->
</section><!-- #primary .content-area -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Run Code Online (Sandbox Code Playgroud)
Bro*_*ice 14
我正在使用searchform.php和search.php文件已经提到过,但在这里我提供了实际的代码.
创建搜索页面 codex页面有助于此处#Creating_a_Search_Page_Template显示搜索查询.
在我的例子中,我将$search_query参数传递给WP_Query Class(可以确定是否是搜索查询!).然后我运行The Loop来显示我想要的帖子信息,在我的例子中是the_permalink和the_title.
搜索框表格:
<form class="search" method="get" action="<?php echo home_url(); ?>" role="search">
<input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder' ) ?>" value="<?php echo get_search_query() ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" />
<button type="submit" role="button" class="btn btn-default right"/><span class="glyphicon glyphicon-search white"></span></button>
</form>
Run Code Online (Sandbox Code Playgroud)
search.php 模板文件:
<?php
global $query_string;
$query_args = explode("&", $query_string);
$search_query = array();
foreach($query_args as $key => $string) {
$query_split = explode("=", $string);
$search_query[$query_split[0]] = urldecode($query_split[1]);
} // foreach
$the_query = new WP_Query($search_query);
if ( $the_query->have_posts() ) :
?>
<!-- the loop -->
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; ?>
</ul>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Run Code Online (Sandbox Code Playgroud)