简体   繁体   English

获取用于查询自定义帖子类型的当前页面自定义分类法

[英]Get the current page custom taxonomy use to query custom post type

So far I have a custom taxonomy that is registered called position that is associated with a specific page as well a custom post type called People . 到目前为止,我有一个自定义分类法,该分类法被注册为与特定页面相关联的称为position ,以及一个名为People的自定义帖子类型。 I also have a page that has the custom taxonomy set to use the position taxonomy as well. 我还有一个页面,该页面已将自定义分类法设置为也使用position分类法。

What I am trying to do is get it so that I can query the custom post type of People to show a list of entries with the custom taxonomy that is set on the page as well as the post type. 我想做的就是获取它,以便我可以查询People的自定义帖子类型,以显示具有在页面上设置的自定义分类法以及帖子类型的条目列表。

For example, the taxonomy of position has some items assigned to it, eg - Sales, Accounting, etc. I have some entries for the custom post type of People eg - John, Mary, Sam, etc. I have John and Mary set to use the taxonomy of Sales , in this case. 例如,分类学position分配了一些项目,例如: -销售,会计,等我有自定义职位类型的某些条目People如-约翰,玛丽,山姆等我有约翰和玛丽设为在这种情况下,请使用Sales的分类法。

On the page of Sales, I have it set to use the custom position taxonomy of Sales . 在Sales页面上,我设置为使用Sales的自定义position分类法。

What I'm trying to get is get the custom taxonomy of position on the Sales template page which should return the name and/or slug of Sales and then take that info and pass it into a query to check against the People custom post type and pull the associated people from that custom taxonomy. 我想要得到的是获得的自定义分类position销售模板页面上应返回的名称和/或蛞蝓Sales ,然后采取这一信息,并把它传递到一个查询所要检查的People自定义后的类型和从相关的习惯分类中删除相关人员。 In this case, on the Sales page it should be able to pull the two people of John and Mary. 在这种情况下,应该可以在“销售”页面上拉开约翰和玛丽的两个人。

So far I have this on the Sales template page: 到目前为止,我在“销售”模板页面上具有以下内容:

<?php $tax = get_terms( array( 'taxonomy' => 'position' ) ); ?>

That gives me the array of the terms but how do I get the name of the associated taxonomy name that is set on the page and then pass that to a query to get the people? 这给了我一系列的术语,但是如何获得在页面上设置的相关分类法名称的名称,然后将其传递给查询以获取人员呢?

Looks like if I use get_terms it gives me everything and just need the one set taxonomy that is for that particular page, assign that to a variable and then use it for a query like this: 看起来,如果我使用get_terms它可以为我提供一切,并且只需要用于该特定页面的一组分类法即可,将其分配给变量,然后将其用于如下查询:

<?php $args = array( 'post_type' => 'people', 'taxonomy' => $tax);
$loop = new WP_Query( $args ); ?>

Ok so I got it figured out eventually. 好的,我最终弄清楚了。

In order to make this work you'd need to get the current post ID from the page, pass that through get_the_terms and then construct a query to query the custom post type with tax_query and then pass that into the tax_query array: 为了完成这项工作,您需要从页面获取当前的帖子ID,将其传递给get_the_terms ,然后构造一个查询以使用tax_query查询自定义帖子类型,然后将其传递给tax_query数组:

<?php
            $id = get_post($post->ID);
            $tax = get_the_terms($id, 'position');
            $args = array(
              'post_type' => 'people',
              'tax_query' => array(
                array(
                  'taxonomy' => 'position',
                  'field'    => 'slug',
                  'terms'    => $tax[0]->slug,
                ),
              ),
            );
            $loop = new WP_Query( $args );
            ?>

That will cross reference the custom taxonomy between the current page and the custom post types and will get those post types that are assigned with the specific taxonomy (in this case, Sales). 这将在当前页面和自定义帖子类型之间交叉引用自定义分类,并获得使用特定分类(在这种情况下为Sales)分配的那些帖子类型。

Hope this helps! 希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从当前帖子(自定义帖子类型)中获取子类别和父类别名称(自定义分类) - Get child and parent category name (custom taxonomy) from current post (custom post type) 在Wordpress中显示自定义帖子类型分类的页面 - Display page of custom post type taxonomy in Wordpress 如何查询自定义帖子类型分类以创建模板页面的分类列表 - How to query a custom post type taxonomy to create a taxonomy list for a template page 为自定义帖子类型类别列表添加类“ current_page_item”(注意:该类别属于自定义分类法) - Adding class “current_page_item” for custom post type category list(note: the category is belong in custom taxonomy) 使用自定义字段查询自定义帖子类型,并按自定义分类升序排列 - Query custom post type with custom field and order ascending by custom taxonomy 如何获取自定义帖子类型的分类值 - How to get the taxonomy values of a custom post type 自定义帖子类型的当前分类法中所有帖子的列表 - list of all post from the current taxonomy of custom post type 类别页面上的自定义帖子类型的自定义分类法 - Custom taxonomy thumnbails for custom post type on category page WP自定义帖子类型查询不返回自定义分类 - WP custom post type query does not return custom taxonomy 带有自定义分类查询循环的Genesis自定义帖子类型分页 - Genesis custom post type pagination with custom taxonomy query loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM