WordPress主题开发中如何实现按分类及时间搜索功能一、创建搜索表单一、制作搜索结果列表页(search.php)

一、[b]创建搜索表单[/b]

1、[b]创建表单[/b]

<form method="get" class="search-form" action="<?PHP bloginfo('home'); ?>">

<select 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>

[b]2、获取分类方法[/b]

1.1手动添加

<select 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');?>

[b]3、表单注意事项[/b]

1.1搜索词输入框的Input的name必须是s;

1.2 form的action必须填写;

一、[b]制作搜索结果列表页(search.PHP[/b]

1、[b]搜索结果页参考代码[/b]

<?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 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;?>

更多更详细的介绍,可以查看wordpress实现按分类及时间搜索功能视频教程

相关文章

我们有时候在定制WORDPRESS主题的时候,由于菜单样式的要求我...
很多朋友在做wordpree主题制作的时候会经常遇到一个问题,那...
wordpress后台的模块很多,但并不是每个都经常用到。介绍几段...
从WordPress4.2版本开始,如果我们在MYSQL5.1版本数据中导出...
很多网友会遇到这样一个问题,就是WordPress网站上传图片、附...
对于经常要在文章中出现代码的IT相关博客,安装一个代码高亮...