Home > database >  Display custom post type categories (Wordpress)
Display custom post type categories (Wordpress)

Time:09-27

I would like to display the categories of the custom post type "ebooks". I have followed several tutorials to achieve this but none of them work. I have no error, only an empty array. I have checked well, there is no problem with the name of the taxonomy or other.

Here is my code to display the categories:

<?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;?>

And 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' ),
    ));
}

CodePudding user response:

Simply Use the below code

$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){

}

CodePudding user response:

Try this code

<?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;?>
  • Related