【问题标题】:Display taxonomy from custom post type从自定义帖子类型显示分类
【发布时间】:2015-03-31 10:42:30
【问题描述】:

我目前有一个“产品”的自定义帖子类型。

如您所见,我现在拥有由以下代码创建的外部和内部产品:

    add_action( 'init', 'create_product__cat_external' );
function create_product__cat_external() {
    register_taxonomy(
        'ExternalProducts',
        'products',
        array(
            'label' => __( 'External Products' ),
            'rewrite' => array( 'slug' => 'externalproducts' ),
            'hierarchical' => true,
        )
    );
}
add_action( 'init', 'create_product__cat_internal' );

function create_product__cat_internal() {
    register_taxonomy(
        'InternalProducts',
        'products',
        array(
            'label' => __( 'Internal Products' ),
            'rewrite' => array( 'slug' => 'internalproducts' ),
            'hierarchical' => true,
        )
    );
}

我想要实现的是一个页面,它只能显示外部产品中的类别。

我有这个 sn-p 代码,它同时显示外部和内部:

<?php
              $customPostTaxonomies = get_object_taxonomies('products');

              if(count($customPostTaxonomies) > 0)
              {
                   foreach($customPostTaxonomies as $tax)
                   {
                         $args = array(
                            'orderby' => 'name',
                            'show_count' => 0,
                            'pad_counts' => 0,
                            'hierarchical' => 1,
                            'taxonomy' => $tax,
                            'title_li' => ''
                            );

                         wp_list_categories( $args );
                   }
             }
             ?>

任何帮助都会很棒。 干杯。

更新:

我目前有分类名称和描述输出,但链接没有链接显示该分类内的帖子。

代码如下:

<?php
             $taxonomy = 'ExternalProducts';
             $queried_term = get_query_var($taxonomy);
             $terms = get_terms($taxonomy, 'slug='.$queried_term);
             if ($terms) {
              echo '<ul>';
              foreach($terms as $term) {
   // The $term is an object, so we don't need to specify the $taxonomy.
                $term_link = get_term_link( $term );

// If there was an error, continue to the next term.
                if ( is_wp_error( $term_link ) ) {
                  continue;
            }

// We successfully got a link. Print it out.
            echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>';
            echo $term->description;
      }
      echo '</ul>';
}
?>

【问题讨论】:

  • 也许您可以使用 'child_of' 参数并传递所需分类的 ID 来仅获取外部或内部术语?
  • 请使用模板层次结构。您可以在其中管理来自特定分类的所有产品。
  • 我会试试那个trainoasis。对不起,我不关注 Riteshdjoshi

标签: php wordpress content-management-system custom-post-type custom-taxonomy


【解决方案1】:

您需要使用函数“get_terms”。这里是简要代码。 假设您的分类名称 = custom_taxonomy

$catlsit = get_terms('custom_taxonomy', array( 'orderby' => 'count', 'hide_empty' => 0 ) ); print_r($catlsit); 您将获得在 taxonomy :custom_taxonomy 中创建的类别列表。 如果您需要其他任何东西,请写下来。 谢谢,

【讨论】:

  • 谢谢,但由于某种原因,我在尝试此操作时遇到多个错误
  • 你能解释一下在什么情况下,你得到了错误吗?我想解决这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-20
相关资源
最近更新 更多