[英]How to display custom post type categories in WordPress?
我想顯示自定義帖子類型“電子書”的類別。 我已經按照幾個教程來實現這一點,但它們都不起作用。 我沒有錯誤,只有一個空數組。 我查得很好,分類名稱或其他名稱沒有問題。
這是我顯示類別的代碼:
<?php
$taxonomy = 'ebooks-category';
$terms = get_terms($taxonomy);
if ( $terms && !is_wp_error( $terms ) ) :
?>
<ul>
<?php foreach ( $terms as $term ) { ?>
<li><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><?php echo $term->name; ?></a></li>
<?php } ?>
</ul>
<?php endif;?>
和functions.php:
function wpm_custom_post_type_ebooks() {
// On rentre les différentes dénominations de notre custom post type qui seront affichées dans l'administration
$labels = array(
// Le nom au pluriel
'name' => _x('Ebooks', 'Post Type General Name'),
// Le nom au singulier
'singular_name' => _x( 'Ebook', 'Post Type Singular Name'),
// Le libellé affiché dans le menu
'menu_name' => __( 'Ebooks'),
// Les différents libellés de l'administration
'all_items' => __( 'All Ebooks'),
'view_item' => __( 'See Ebooks'),
'add_new_item' => __( 'Add New Ebook'),
'add_new' => __( 'Add Ebook'),
'edit_item' => __( 'Edit Ebook'),
'update_item' => __( 'Update Ebook'),
'search_items' => __( 'Search Ebook'),
'not_found' => __( 'Not found'),
'not_found_in_trash' => __( 'Not found in trash'),
);
// On peut définir ici d'autres options pour notre custom post type
$args = array(
'label' => __( 'Ebooks'),
'description' => __( 'All Ebooks'),
'labels' => $labels,
'menu_icon' => 'dashicons-book-alt',
// On définit les options disponibles dans l'éditeur de notre custom post type ( un titre, un auteur...)
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
/*
* Différentes options supplémentaires
*/
'show_in_rest' => true,
'hierarchical' => false,
'public' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'ebooks'),
);
// On enregistre notre custom post type qu'on nomme ici "serietv" et ses arguments
register_post_type( 'ebooks', $args );
}
add_action( 'init', 'wpm_custom_post_type_ebooks', 0 );
add_action( 'init', 'create_category_hierarchical_taxonomy_ebooks', 0 );
//create a custom taxonomy name it subjects for your posts
function create_category_hierarchical_taxonomy_ebooks() {
// Add new taxonomy, make it hierarchical like categories
//first do the translations part for GUI
$labels = array(
'name' => _x( 'ebooks-category', 'taxonomy general name' ),
'singular_name' => _x( 'Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Categories' ),
'all_items' => __( 'All Categories' ),
'parent_item' => __( 'Parent Category' ),
'parent_item_colon' => __( 'Parent Category:' ),
'edit_item' => __( 'Edit Category' ),
'update_item' => __( 'Update Category' ),
'add_new_item' => __( 'Add New Category' ),
'new_item_name' => __( 'New Category Name' ),
'menu_name' => __( 'Category' ),
);
// Now register the taxonomy
register_taxonomy('ebooks-category', array('ebooks'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_in_rest' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'category' ),
));
}
對於自定義分類,您必須為 get_terms() 使用一些額外的參數。
<?php
$taxonomy = 'ebooks-category';
$terms = get_terms(array(
'taxonomy' => $taxonomy,
'hide_empty' => false
));
if ( $terms && !is_wp_error( $terms ) ) :
?>
<ul>
<?php foreach ( $terms as $term ) { ?>
<li><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><?php echo $term->name; ?></a></li>
<?php } ?>
</ul>
<?php endif;?>
只需使用以下代碼:
$categories = get_categories(array(
'orderby' => 'name',
'parent' => 0,
'order' => 'ASC',
'taxonomy' => 'product-category', //This is custom taxonomy replace the value with yours
'post_type' => 'products', // and this is the post type of that replace it with your post type
'hide_empty' => false
));
foreach ($categories as $category) {
}
試試這個代碼
<?php
$terms = get_terms( array(
'hide_empty' => 0,
'taxonomy' => 'ebooks-category',
) );
if ( $terms && !is_wp_error( $terms ) ) :
?>
<ul>
<?php foreach ( $terms as $term ) { ?>
<li><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><?php echo $term->name; ?></a></li>
<?php } ?>
</ul>
<?php endif;?>
我們使用 'hide_empty' => 0 來顯示所有分類術語,即使它沒有附加書籍
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.