一、创建搜索表单
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;?>
更多更详细的介绍,可以查看WordPress实现按分类及时间搜索功能视频教程
WordPress日记主要承接WordPress主题定制开发、PSD转WordPress、WordPress仿站以及以WordPress为管理后端的小程序、APP,我们一直秉持“做一个项目,交一个朋友”的理念,希望您是我们下一个朋友。如果您有WordPress主题开发需求,可随时联系QQ:919985494
相关文章
WordPress订阅者或投稿者上传图片提示:抱歉,您不能添加附件到此文章。
一直以来关于WordPress投稿者无法上传图片的问题都困扰…wordpress主题禁用F12或审查元素
我们很多时候费劲千辛万苦做好了一个wordpress主题,不…如何开启WordPress基础调试外的更多高级调试功能?
调试WordPress是从WP_DEBUG开始的,但是还有更…WordPress主题开发中同一个文章列表中实现多种样式
WordPress主题开发过程中,可以在文章列表中实现多种样…WordPress创建自定义文章类型分类模板和文章页模板
有时候我们需要创建一个自定义文章类型来让网站管理更加方便,而…WordPress导入数据库出现”Unknown collation: ‘utf8mb4_unicode_ci”之类错误的解决办法
从WordPress4.2版本开始,如果我们在MYSQL5.…