首页 文章

如何在Wordpress中使用Taxonomy类别创建过滤器

提问于
浏览
0

我需要创建一个带有Wordpress Taxonomy Categories的过滤器,其帖子就像这张图片
question
Name,Issuer,Isin和Market place下拉列表是类别 . 我如何分类并用他们的帖子创建一个简单的过滤器?我搜索了更多但不知道如何管理和从哪里开始 . 尝试插件Beautiful taxonomy filters但它没有我需要的单独的类别 . 请帮帮我 . 我正在这样下拉

function get_terms_dropdown($taxonomies, $args){
                                    $myterms = get_terms($taxonomies, $args);
                                    $output ="<select class='news_cat'>";
                                    foreach($myterms as $term){
                                        $root_url = get_bloginfo('url');
                                        $term_taxonomy=$term->taxonomy;
                                        $term_slug=$term->slug;
                                        $term_name =$term->name;
                                        $link = $root_url.'/?'.$term_taxonomy.'='.$term_slug;
                                        $output .="<option value='".$link."'>".$term_name."</option>";
                                    }
                                    $output .="</select>";
                                return $output;
                                }
                    $taxonomies = array('articles-tax');
                    $args = array('orderby'=>'count','hide_empty'=>false);
                    echo get_terms_dropdown($taxonomies, $args);

并以这种方式创建了一个表单

<form role="search" method="get" id="searchform" action="<?php bloginfo('url'); ?>">and dropdowns here</form>

但仍然无法得到并在网址中看到所有选定的下拉值,请帮助我解决这个问题,这是我在过滤世界中的第一步!

1 回答

  • 0

    SEARCH 按钮上的URL上传递所有术语 nameslug . 获取QUERY_STRING并使用get_term_by函数搜索Term ID:

    // Suppose you have term name in URL
    // Then find Term ID with Name, or if you have slug in url then change "name" to "slug"
    $term = get_term_by( "name", $term_name, $taxonomy );
    //$term->term_id;
    

    现在,您必须使用WP_Query按特定术语ID访问帖子:

    $args = array(
        'post_type' => 'YOUR_POST_TYPE',
        'tax_query' => array(
            array(
              'taxonomy' => $taxonomy,
              'field' => 'id',
              'terms' => $term->term_id // You can pass more then one term id like array(32, 65)
            )
        )
    );
    $query = new WP_Query( $args );
    

相关问题