简体   繁体   English

WordPress > 从自定义帖子类型获取自定义分类法

[英]WordPress > Get custom taxonomy from a custom post type

  1. Installed Custom Post Type UI wordpress plugin安装自定义帖子类型 UI wordpress 插件
  2. Created a custom post type = 'products'创建自定义帖子类型 = 'products'
  3. Registered a custom taxonomy categories (different from category) using使用注册自定义分类类别(不同于类别)

1. How to display all custom post types and have a filter above with categories that works as filter tabs? 1.如何显示所有自定义帖子类型并在上方设置过滤器,其中类别可用作过滤器选项卡?

2.How to loop in a custom template through the custom taxonomy 'categories' and display links? 2.如何通过自定义分类“类别”和显示链接在自定义模板中循环?

HTML structure HTML 结构

Custom Post type URL : /wp-admin/edit-tags.php?taxonomy=categories&post_type=products自定义帖子类型 URL:/wp-admin/edit-tags.php?taxonomy=categories&post_type=products

PHP PHP

<?php get_term( $term, $taxonomy, $output, $filter ) ?>

<?php 
$args=array(
  'name' => 'categories'
);
$output = 'products'; // or names
$taxonomies=get_taxonomies($args,$output); 
if  ($taxonomies) {
  foreach ($taxonomies  as $taxonomy ) {
    echo '<p>' . $taxonomy->name . '</p>';
  }
}  
?>

<?php 
$args = array(
  'public'   => true,
  '_builtin' => false

); 
$output = 'names'; // or objects
$operator = 'and'; // 'and' or 'or'
$taxonomies = get_taxonomies( $args, $output, $operator ); 
if ( $taxonomies ) {
  foreach ( $taxonomies  as $taxonomy ) {
    echo '<p>' . $taxonomy . '</p>';
  }
}

You can do this to get all the terms of a custom taxonomy:您可以这样做以获取自定义分类法的所有术语:

https://developer.wordpress.org/reference/functions/get_terms/ https://developer.wordpress.org/reference/functions/get_terms/

$terms = get_terms( array(
    'taxonomy' => 'categories',
    'hide_empty' => false,
) );

foreach ( $terms as $term ) {
    $term_link = get_term_link( $term );
}

$term returns an array with the following structure: $term返回一个具有以下结构的数组:

array(
[0] => WP_Term Object
(
    [term_id] =>
    [name] =>
    [slug] =>
    [term_group] =>
    [term_taxonomy_id] =>
    [taxonomy] =>
    [description] =>
    [parent] =>
    [count] =>
    [filter] =>
)

$term_link will give you the permalink to your taxonomy term archive. $term_link将为您提供分类术语档案的永久链接。

https://developer.wordpress.org/reference/functions/get_term_link/ https://developer.wordpress.org/reference/functions/get_term_link/


Regarding your other question of how to implement filter tabs: Check out this plugin: https://wordpress.org/plugins/beautiful-taxonomy-filters/关于如何实现过滤器选项卡的其他问题:查看此插件: https : //wordpress.org/plugins/beautiful-taxonomy-filters/

To find the taxonomies associated with a given post type, use the WordPress get_object_taxonomies() function like this:要查找与给定帖子类型相关的分类法,请使用 WordPress get_object_taxonomies()函数,如下所示:

$taxonomies = get_object_taxonomies('post', 'objects');

$taxonomies will be an array of WP_Taxonomy objects. $taxonomies将是一组WP_Taxonomy对象。 Omit the second argument to get an array of post_type slugs.省略第二个参数以获取 post_type slug 数组。

To query by a custom taxonomy, create a new WP_Query something like this:要通过自定义分类进行查询,请创建一个新的 WP_Query,如下所示:

$args = [
    'posts_per_page' => -1,
    'tax_query' => [
        [
            'taxonomy' => 'categories',
            'field' => 'slug',
            'terms' => ['your-term'],
        ],
    ],
];
$filterQuery = new WP_Query($args);

While taxonomy associations can be declared on post_types using register_post_type , the association is optional and very weak.虽然可以使用register_post_type在 post_types 上声明分类关联,但关联是可选的并且非常弱。 Internally, WordPress goes the other way and assigns post_types to Taxonomies.在内部,WordPress 采取另一种方式并将 post_types 分配给分类法。

Each Taxonomy has an object_type property which is an array of slugs for the post_types it knows about.每个分类法都有一个object_type属性,它是它所知道的 post_types 的 slug 数组。 Digging into the register_post_type source code, we see it calls register_taxonomy_for_object_type for each item in the taxonomies argument property, then simply adds a slug to the Taxonomy's object_type array.深入研究register_post_type源代码,我们看到它为taxonomies参数属性中的每个项目调用register_taxonomy_for_object_type ,然后简单地向分类法的object_type数组添加一个 slug。 This is the only time the post_type's taxonomy property is used.这是唯一一次使用 post_type 的分类属性。 I prefer to declare post_types when registering the Taxonomy since it's closer to how WordPress works and misunderstanding that association caused me a lot of grief in the past.我更喜欢在注册分类法时声明 post_types,因为它更接近 WordPress 的工作方式,并且误解了这种关联在过去给我带来了很多悲伤。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM