【问题标题】:How to create a filter with Taxonomy Categories in Wordpress如何在 Wordpress 中使用分类类别创建过滤器
【发布时间】:2015-05-02 13:00:32
【问题描述】:

我需要使用 Wordpress 分类法类别及其帖子创建一个过滤器,就像这张图片 名称、发行人、Isin 和市场下拉列表是类别。我如何分隔类别并使用他们的帖子创建一个简单的过滤器?我搜索了更多但不知道如何管理以及从哪里开始。尝试了插件 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>

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

【问题讨论】:

    标签: php wordpress filter taxonomy


    【解决方案1】:

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

    【讨论】:

    • 你的意思是说通过 url @jogesh_pi 上的所有分类名称?
    • @AnahitGhazaryan 哎呀,这是一个拼写错误,它的术语名称
    • 因此,例如,我在下拉列表中获取了这些类别并在其后设置样式我应该做什么@jogesh_pi?
    • @AnahitGhazaryan,对于下拉列表,您必须使用
    • 然后在提交时你会得到?_name=term_slug&amp;_issuer=term_slug&amp;_isn=term_slug这样的url然后通过$_GET或任何首选方法检测它并根据代码申请
    猜你喜欢
    • 2011-08-23
    • 2019-08-11
    • 2015-02-25
    • 2015-04-17
    • 2011-10-29
    • 2013-01-12
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    相关资源
    最近更新 更多