在存档页面上添加一个DropDown分类过滤器 [英] Add a DropDown Taxonomy Filter on Archive Page
本文介绍了在存档页面上添加一个DropDown分类过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是分类法的HTML输出:
/ p>
< form method =postaction =<?php echo $ _SERVER ['PHP_SELF'];?> >
< option value =>选择服务< / option>
< option value =密集支持>强化支持< / option>
< option value =recovery-work>恢复工作< / option>
< option value =tenant-support> Tenant Support< / option>
< / select>
< / form>
以下是查询码:
< table class =dataTable>
< tr>
< th>名称< / th>
< th>位置< / th>
< th>网站< / th>
服务(s)< / th>
< / tr>
<?php $ service_query = new WP_Query('post_type = ptype_service& posts_per_page = -1'); ?>
<?php $ j = 0?>
<?php while($ service_query-> have_posts()):$ service_query-> the_post(); ?>
<?php $ additional_class =(++ $ j%2 == 0)? 'even':'odd'; ?>
< tr class =<?php echo $ additional_class?>>
< td><?php the_title()?>< / td>
< td><?php echo(get_post_meta($ post-> ID,'pir_groups_location',true))?>< / td>
< td>< a target =_ blankhref =http://<?php echo(get_post_meta($ post-> ID,'pir_groups_website',true))?>> ;<?php echo(get_post_meta($ post-> ID,'pir_groups_website',true))?>< / a>< / td>
< td>
<?php
$ terms = get_the_terms($ post-> ID,'services');
if($ terms&&!is_wp_error($ terms)):
$ services_links = array();
foreach($ terms为$ term){
$ services_links [] = $ term-> name;
}
$ on_draught = join(,,$ services_links);
?>
<?php echo $ on_draught; ?>
<?php endif; ?>
< / td>
< / tr>
<?php endwhile; ?>
< / table>
问候
解决方案自己找到解决方案:)
刚刚改变了表单动作,现在它工作正常:
< form method =postaction =<?php the_permalink()?>>
< option value =>所有服务< / option>
<?php
$ terms = get_terms('services');
if($ terms){
foreach($ terms as $ term){?>
< option<?php if($ term-> slug == $ _POST ['services']){echo'selected =selected';}?> value =<?php echo esc_attr($ term-> slug)?>><?php echo esc_html($ term-> name)?>< / option>
<?php}
}
?>
< / select>
< / form>
I need to add a taxonomy dropdwon filter in archive page so that I can display the posts assigned to selected taxonomy.
Here is the HTML Output for taxonomies:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="services" id="selectservice" class="postform" onchange="submit();">
<option value="">Select Service</option>
<option value="intensive-support">Intensive Support</option>
<option value="recovery-work">Recovery Work</option>
<option value="tenant-support">Tenant Support</option>
</select>
</form>
Below is the Query Code :
<table class="dataTable">
<tr>
<th>Name</th>
<th>Location</th>
<th>Website</th>
<th>Service(s)</th>
</tr>
<?php $service_query = new WP_Query('post_type=ptype_service&posts_per_page=-1'); ?>
<?php $j = 0 ?>
<?php while ($service_query->have_posts()) : $service_query->the_post(); ?>
<?php $additional_class = (++$j % 2 == 0) ? 'even' : 'odd'; ?>
<tr class="<?php echo $additional_class ?>">
<td><?php the_title()?></td>
<td><?php echo (get_post_meta($post->ID, 'pir_groups_location', true))?></td>
<td><a target="_blank" href="http://<?php echo (get_post_meta($post->ID, 'pir_groups_website', true))?>"><?php echo (get_post_meta($post->ID, 'pir_groups_website', true))?></a></td>
<td>
<?php
$terms = get_the_terms( $post->ID, 'services' );
if ( $terms && ! is_wp_error( $terms ) ) :
$services_links = array();
foreach ( $terms as $term ) {
$services_links[] = $term->name;
}
$on_draught = join( ", ", $services_links );
?>
<?php echo $on_draught; ?>
<?php endif; ?>
</td>
</tr>
<?php endwhile; ?>
</table>
Regards
解决方案
Found Solution myself :)
Just Changed the form action and its working fine now :
<form method="post" action="<?php the_permalink()?>">
<select name="services" id="selectservice" class="postform" onchange="submit();">
<option value="">All Services</option>
<?php
$terms = get_terms('services');
if ( $terms ) {
foreach ( $terms as $term ) {?>
<option <?php if($term->slug == $_POST['services']){ echo 'selected="selected"';} ?> value="<?php echo esc_attr( $term->slug )?>"><?php echo esc_html( $term->name ) ?></option>
<?php }
}
?>
</select>
</form>
这篇关于在存档页面上添加一个DropDown分类过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文