WordPress主题开发实现按分类及时间搜索功能教程

分享一下WordPress主题开发实现按分类及时间搜索功能教程。
WordPress主题开发实现按分类及时间搜索功能教程 (https://www.wp-admin.cn/) WordPress开发教程 第1张
一、创建搜索表单

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

相关推荐

发表回复

房先生
我们将24小时内回复。
2024-07-24 02:31:18
您好,有任何疑问请与我们联系!
您的工单我们已经收到,我们将会尽快跟您联系!
[QQ客服]
2781198
加我微信
[电话联系]
13585372227
[电子邮件]
chaoneo@163.com
取消

选择聊天工具: