【问题标题】:WordPress > Get custom taxonomy from a custom post typeWordPress > 从自定义帖子类型获取自定义分类
【发布时间】:2016-10-28 05:24:14
【问题描述】:
  1. Installed Custom Post Type UI wordpress plugin
  2. 创建了自定义帖子类型 = 'products'
  3. 使用
  4. 注册了一个自定义分类类别(不同于类别)

1.如何显示所有自定义帖子类型并在上方设置一个过滤器,其中包含用作过滤器标签的类别?

2.如何通过自定义分类“类别”循环进入自定义模板并显示链接?

HTML 结构

自定义帖子类型网址: /wp-admin/edit-tags.php?taxonomy=categories&post_type=products

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>';
  }
}

【问题讨论】:

    标签: wordpress post types filter


    【解决方案1】:

    要查找与给定帖子类型关联的分类法,请使用 WordPress get_object_taxonomies() 函数,如下所示:

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

    $taxonomies 将是 WP_Taxonomy 对象的数组。省略第二个参数以获取分类 slug 数组。

    要通过自定义分类进行查询,请创建一个新的 WP_Query,如下所示:

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

    虽然可以使用 register_post_type 在 post_types 上声明分类关联,但关联是可选的并且非常弱。在内部,WordPress 采用另一种方式,将 post_types 分配给分类法。

    每个分类法都有一个 object_type 属性,它是它所知道的 post_types 的 slug 数组。深入研究register_post_type 源代码,我们看到它为taxonomies 参数属性中的每个项目调用register_taxonomy_for_object_type,然后简单地将一个slug 添加到分类法的object_type 数组中。这是唯一一次使用 post_type 的分类属性。我更喜欢在注册分类时声明 post_types,因为它更接近 WordPress 的工作方式,并且误解了这种关联在过去给我带来了很多悲伤。

    【讨论】:

    • 正是我想要的
    【解决方案2】:

    您可以这样做来获取自定义分类的所有术语:

    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 返回一个结构如下的数组:

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

    $term_link 将为您提供您的分类术语存档的永久链接。

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


    关于如何实现过滤选项卡的其他问题: 看看这个插件: https://wordpress.org/plugins/beautiful-taxonomy-filters/

    【讨论】:

    • 我试过了,但它仍然没有给我任何东西,而且你没有在任何地方指定自定义帖子类型。
    • 您不需要帖子类型来获取分类条款,但如果您的 WP 版本早于 4.5.0,您需要进行一些小改动:$terms = get_terms( 'categories', array( 'hide_empty' =&gt; false, ) );
    猜你喜欢
    • 2013-08-03
    • 2011-06-26
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-02
    相关资源
    最近更新 更多