WordPress主题开发实现按分类及时间搜索功能教程
分享一下WordPress主题开发实现按分类及时间搜索功能教程。
一、创建搜索表单
1、创建表单
<form method=”get” class=”search-form” action=”<?php bloginfo(‘home’); ?>”>
<select style=”” name=”cat” class=”search-option”>
<option value=”0″ selected=”selected”>全部</option>
<option value=”1″>新闻动态</option>
<option value=”2″>行业动态</option>
</select>
<input class=”bgtime” name=”bgtime” type=”date”>
<input class=”endtime” name=”endtime” type=”date”>
<input type=”search” class=”search-field” placeholder=”请输入关键词” value=”” name=”s” />
<input type=”submit” class=”search-submit” value=”搜索” />
</form>
2、获取分类的方法
1.1手动添加
<select style=”” name=”cat” class=”search-option”>
<option value=”0″ selected=”selected”>全部</option>
<option value=”1″>新闻动态</option>
<option value=”2″>行业动态</option>
</select>
1.2使用wp_dropdown_categories()函数
<?php wp_dropdown_categories(‘class=search_select&show_option_all=全站搜索&orderby=name&hierarchical=0&selected=-1&hide_empty=0&depth=1’);?>
3、表单注意事项
1.1搜索词输入框的Input的name必须是s;
1.2 form的action必须填写;
一、制作搜索结果列表页(search.php)
1、搜索结果页参考代码
<?php
$args = array(
‘cat’ => $_GET[‘cat’],
‘post_type’ => ‘post’,
‘date_query’ => array(
array(
‘after’ => $_GET[‘bgtime’],
‘before’ => $_GET[‘endtime’],
‘inclusive’ => true,
),
),
‘s’ => $_GET[‘s’],
);
$the_query = new WP_Query( $args );?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while($the_query->have_posts()):$the_query->the_post();?>
<div class=”listone”>
<div class=”listright”>
<a href=”<?php%20the_permalink();?>” title=”<?php the_title();?>”>
<?php the_title();?><span style=”float: right;”><?php the_time(‘Y-m-d’);?></span>
</a>
</div>
<div class=”clearfix”></div>
</div>
<?php endwhile;?>
<?php else :?>
<div class=”nolist”>No relevant content</div>
<?php endif;?>