【问题标题】:Add a DropDown Taxonomy Filter on Archive Page在存档页面上添加下拉分类过滤器
【发布时间】:2013-09-26 08:19:45
【问题描述】:

我需要在存档页面中添加分类 dropdwon 过滤器,以便我可以显示分配给所选分类的帖子。

这是分类法的 HTML 输出:

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

下面是查询代码:

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

问候

【问题讨论】:

    标签: wordpress filter custom-post-type taxonomy term


    【解决方案1】:

    自己找到解决方案:)

    刚刚更改了表单操作,现在可以正常工作了:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-25
      • 1970-01-01
      • 1970-01-01
      • 2016-04-01
      • 2019-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多