Wordpress 使用WP_Term_查询获取自定义分类法,但仅显示post中的分类法
我将用以下代码显示所有分类:Wordpress 使用WP_Term_查询获取自定义分类法,但仅显示post中的分类法,wordpress,categories,taxonomy,custom-taxonomy,Wordpress,Categories,Taxonomy,Custom Taxonomy,我将用以下代码显示所有分类: 'orderby' => 'ASC', 'post_type' => 'integrations_cpt', 'taxonomy' => 'categories_integration', 'hide_empty' => false, ); $the_query = new WP_Query( $args ); $categories = new WP_Ter
'orderby' => 'ASC',
'post_type' => 'integrations_cpt',
'taxonomy' => 'categories_integration',
'hide_empty' => false,
);
$the_query = new WP_Query( $args );
$categories = new WP_Term_Query( $args );
使用get_terms()函数查找所有类别
$terms = get_terms('categories_integration');
foreach ($terms as $cat) {
echo $cat->name;
}
注意:尝试在第39行的/var/www/html/wp content/themes/sendinblue2019/template parts/integrations/section-browse-plugin.php中获取非对象的属性“name”如果我尝试var_dump($terms),响应是:object(wp#Error){[“errors”]=>array(1){[“invalid#taxonomy”]=>array(1){[0]=>string(17)“invalid taxonomy.”[“error_data”]=>array(0){},所以WordPress版本中可能有错误。请根据您的评论检查此链接。我发现问题是使用了双主题,代码很好
function create_integrations_custom_post_type()
{
register_post_type('integrations_cpt',
array(
'labels' => array(
'name' => __('Integrations'),
'singular_name' => __('Integration')
),
'public' => true,
'menu_icon' => 'dashicons-share',
'query_var' => true,
'has_archive' => true,
'taxonomies' => array('categories_integration'),
'supports' => array('custom-fields','title'),
)
);
register_taxonomy( 'categories_integration', 'integrations_cpt', array(
'hierarchical' => true,
'label' => 'Categories',
'query_var' => true,
'exclude_from_search' =>false,
'singular_label' => 'Category',
'has_archive' => true,
)
);
register_taxonomy_for_object_type( 'categories_integration', 'integrations_cpt' );
}
add_action('init', 'create_integrations_custom_post_type');
$terms = get_terms('categories_integration');
foreach ($terms as $cat) {
echo $cat->name;
}