首先是页面结构部分
<div class="multiple_search_form"> <form method="get"> <!-- 输出当前分类页面下的子分类,以下拉菜单的形式输出 --> <div class="category-group"> <?php if ( is_category() ) { $this_category = get_category($cat); if($this_category->category_parent): else: $args = array( 'current_category' => 1, 'show_option_all' => 'All Categories', 'show_option_none' => '', 'option_none_value' => '-1', 'orderby' => 'ID', 'order' => 'ASC', 'show_count' => 0, 'hide_empty' => 0, 'child_of' => $this_category->cat_ID, 'exclude' => '', 'include' => '', 'echo' => 1, 'selected' => 0, 'hierarchical' => 0, 'name' => 'cat', 'id' => '', 'class' => 'postform', 'depth' => 0, 'tab_index' => 0, 'taxonomy' => 'category', 'hide_if_empty' => false, 'value_field' => 'term_id', ); $this_category = wp_dropdown_categories( $args ); endif; } ?> </div> <!-- 输出本站所有的标签,以下拉菜单的形式输出 --> <div class="tags-group"> <?php function dropdown_tag_cloud( $args = '' ) { $defaults = array( 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45, 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC', 'exclude' => '', 'include' => '' ); $args = wp_parse_args( $args, $defaults ); $tags = get_tags( array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); if ( empty( $tags ) ) return; $return = dropdown_generate_tag_cloud( $tags, $args ); if ( is_wp_error( $return ) ) return false; else echo apply_filters( 'dropdown_tag_cloud', $return, $args ); } function dropdown_generate_tag_cloud( $tags, $args = '' ) { global $wp_rewrite; $defaults = array( 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45, 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC' ); $args = wp_parse_args( $args, $defaults ); extract( $args ); if ( !$tags ) return; $counts = $tag_links = array(); foreach ( ( array )$tags as $tag ) { $counts[ $tag->name ] = $tag->count; $tag_links[ $tag->name ] = get_tag_link( $tag->term_id ); if ( is_wp_error( $tag_links[ $tag->name ] ) ) return $tag_links[ $tag->name ]; $tag_ids[ $tag->name ] = $tag->term_id; } $min_count = min( $counts ); $spread = max( $counts ) - $min_count; if ( $spread <= 0 ) $spread = 1; $font_spread = $largest - $smallest; if ( $font_spread <= 0 ) $font_spread = 1; $font_step = $font_spread / $spread; if ( 'name' == $orderby ) uksort( $counts, 'strnatcasecmp' ); else asort( $counts ); if ( 'DESC' == $order ) $counts = array_reverse( $counts, true ); $a = array(); $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : ''; foreach ( $counts as $tag => $count ) { $tag_id = $tag_ids[ $tag ]; //$tag_link = clean_url($tag_links[$tag]); $tag = str_replace( ' ', ' ', wp_specialchars( $tag ) ); $a[] = "<option value='$tag_id'>$tag</option>"; } switch ( $format ): case 'array': $return = & $a; break; case 'list': $return = "<ul class='wp-tag-cloud'><li>"; $return .= join( "</li><li>", $a ); $return .= "</li></ul>"; break; default: $return = join( "", $a ); break; endswitch; return apply_filters( 'dropdown_generate_tag_cloud', $return, $tags, $args ); } ?> <select name="the_tag_group" id="the_tag_group"> <option value="0">All tags</option> <?php dropdown_tag_cloud('number=0&order=asc'); ?> </select> </div> <!-- 输出本站文章所有的年份,以下拉菜单的形式输出 --> <div class="years-group"> <?php $args = array( 'type' => 'yearly', 'limit' => '', 'format' => 'option', 'before' => '', 'after' => '', 'show_post_count' => false, 'echo' => 1, 'order' => 'DESC', 'post_type' => 'post' ); echo '<select name="the_years_group" id="the_years_group"><option value="0"> All years </option>'; wp_get_archives( $args ); echo '</select>'; ?> </div> <div class="search-group"><input type="submit" value="Search"/> </div> </form> </div>
处理表单提交的参数
global $wpdb; $the_tag_id = $_GET[ 'the_tag_group' ]; $the_cat_id = $_GET[ 'cat' ]; $the_years_string = $_GET[ 'the_years_group' ]; // 将标签的年份输出 function getYearString( $str ) { return preg_match( '/([0-9]{4})/', $str, $a ) ? $a[ 1 ] : 0; // 正则表达式从字符串中筛选出连续的4个数字,通常为年份 } // 执行查询 $args = array( 'post_type' => 'post', 'relation' => 'AND', 'tag_id' => $the_tag_id, 'cat' => $the_cat_id, 'date_query' => array( array( 'year' => getYearString( $the_years_string ) ), ), ); $searched_posts = new WP_Query( $args ); if ( $searched_posts->have_posts() ) { while ( $searched_posts->have_posts() ) { $searched_posts->the_post(); ?> <!-- 循环输出语句 --> <?php }}else{ ?> Nothing matched . <?php }
下面的代码是通过URL参数控制当前下拉菜单的选中状态。
function getQueryString( name ) {
var reg = new RegExp( '(^|&)' + name + '=([^&]*)(&|$)', 'i' );
var r = window.location.search.substr( 1 ).match( reg );
if ( r != null ) {
return unescape( r[ 2 ] );
}
return null;
}
// 这样调用:
var catId = getQueryString( "cat" );
var tagId = getQueryString( "the_tag_group" );
var yearNum = getQueryString( "the_years_group" );
jQuery( ".category-group" ).find( 'option[value=' + catId + ']' ).attr( "selected", "selected" );
jQuery( ".tags-group" ).find( 'option[value=' + tagId + ']' ).attr( "selected", "selected" );
jQuery( "#the_years_group" ).find( 'option[value='+'"'+yearNum+'"'+']' ).attr( "selected", "selected" );
评论区
发表新的留言
您可以留言提出您的疑问或建议。
您的留言得到回复时,会通过您填写的邮箱提醒您。