【问题标题】:get_terms() returns Invalid Taxonomyget_terms() 返回无效分类法
【发布时间】:2018-09-21 11:47:59
【问题描述】:

我查看了其他示例和解决方案,但肯定有一些我遗漏的东西。我已经尝试了其他帖子答案所建议的所有内容,但无济于事。

我不确定到底是什么问题,但我可以在不带任何参数的情况下使用get_terms,并且我可以在我的网站上获取每个术语,但显然我想将其隔离为仅声明的分类法。我在循环之外运行它,所以我不能传递 ID,也不想传递 ID。

任何见解都是有帮助的!

我在使用 get_terms() 时得到了这个。

object(WP_Error)#992 (2) {
  ["errors"]=>
  array(1) {
    ["invalid_taxonomy"]=>
    array(1) {
      [0]=>
      string(17) "Invalid taxonomy."
    }
  }
  ["error_data"]=>
  array(0) {
  }
}

我的代码:

分类

if ( ! function_exists( 'car_ctax_catalog' ) ) {
  // Register Custom Taxonomy
  function car_ctax_catalog() {

    $labels = array(
      'name'                       => _x( 'Categories', 'Taxonomy General Name', 'carmon' ),
      'singular_name'              => _x( 'Category', 'Taxonomy Singular Name', 'carmon' ),
      'menu_name'                  => __( 'Category', 'carmon' ),
      'all_items'                  => __( 'All Categories', 'carmon' ),
      'parent_item'                => __( 'Parent Category', 'carmon' ),
      'parent_item_colon'          => __( 'Parent Category:', 'carmon' ),
      'new_item_name'              => __( 'New Category Name', 'carmon' ),
      'add_new_item'               => __( 'Add New Category', 'carmon' ),
      'edit_item'                  => __( 'Edit Category', 'carmon' ),
      'update_item'                => __( 'Update Category', 'carmon' ),
      'view_item'                  => __( 'View Category', 'carmon' ),
      'separate_items_with_commas' => __( 'Separate categories with commas', 'carmon' ),
      'add_or_remove_items'        => __( 'Add or remove categories', 'carmon' ),
      'choose_from_most_used'      => __( 'Choose from the most used', 'carmon' ),
      'popular_items'              => __( 'Popular Categories', 'carmon' ),
      'search_items'               => __( 'Search Categories', 'carmon' ),
      'not_found'                  => __( 'Not Found', 'carmon' ),
      'no_terms'                   => __( 'No categories', 'carmon' ),
      'items_list'                 => __( 'Categories list', 'carmon' ),
      'items_list_navigation'      => __( 'Categories list navigation', 'carmon' ),
    );
    $args = array(
      'labels'                     => $labels,
      'hierarchical'               => true,
      'public'                     => true,
      'show_ui'                    => true,
      'show_admin_column'          => true,
      'show_in_nav_menus'          => true,
      'show_tagcloud'              => false,
      'rewrite'                    => false,
      'show_in_rest'               => true,
      'rest_base'                  => 'ctax_catalog',
    );
    register_taxonomy( 'ctax_catalog', array( 'cpt_catalog' ), $args );

  }
  add_action( 'init', 'car_ctax_catalog', 0 );
}

获取条款

$args = array (
    'post_type' => 'catalog',
    'taxonomy'  =>  'ctax_catalog',
    'hide_emoty'  =>  false
);
$tax = array ( 'ctax_catalog' );

$terms  = get_terms($tax, $args);
echo"<pre>";var_dump($terms);echo"</pre>";

编辑

我也试过这个,结果是一样的。

获取条款尝试 2

$terms  = get_terms('ctax_catalog');
$tterms = get_terms( array ('taxonomy' => 'ctax_catalog' ) );
echo"<pre>";var_dump($terms);echo"</pre>";
echo"<pre>";var_dump($tterms);echo"</pre>";

【问题讨论】:

标签: wordpress


【解决方案1】:

我想出了以下 hackish 变通办法,直到找到更好的解决方案:

$terms = get_terms();
$gradeOptions = "";

foreach ($terms as $term) {
    if ($term->taxonomy === "pa_grade") {
        $gradeOptions .= "<options value=\"{$term->term_id}\">{$term->name}</options>";
    }
}
var_dump($gradeOptions);
die();

【讨论】:

  • 看,我可以通过这个,但它完全使get_terms('custom_tax')函数无效:\
  • 我完全同意!虽然我无法让它工作,甚至通过 get_terms 函数代码。
猜你喜欢
  • 2015-09-17
  • 1970-01-01
  • 2022-09-27
  • 2012-02-07
  • 2016-08-09
  • 1970-01-01
  • 2019-03-06
  • 1970-01-01
  • 2015-11-14
相关资源
最近更新 更多