WordPress Taxonomy get category

Hopefully an easy fix for someone!

I have a custom post type, named ‘products’.

Read More

Using this code, I am getting the name of each ‘product’:

    <?php 

//Define your custom post type name in the arguments

$args = array('post_type' => 'products');

//Define the loop based on arguments

$loop = new WP_Query( $args );

//Display the contents

while ( $loop->have_posts() ) : $loop->the_post();
?>
<h1 class="entry-title"><?php the_title(); ?></h1>




<?php endwhile;?>

Each of these products is inside a category. What I want to do is get not only the_title, but get the category.

I tried using <?php the_category(); ?> but no luck.

Anyone got any clue?

EDIT:

Sorry, I have these created inside my products custom post type.

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

which give me external and internal products as categories. But inside these, I have categories which have been made on the wordpress backend.

This is how it looks when I select a product:

backend

Related posts

Leave a Reply

2 comments

  1. This is a bit difficult because you attached two different taxonomies to your post type ‘products’. Since your taxonomy is hierarchical it would have been easier to have put everything in one taxonomy with ‘Internal Prodcuts’ and ‘External Products’ on the top level. In you case you will have to check for both of them – I assume that your ‘products‘ can’t be ‘internal‘ AND ‘external‘ at the same time:

    <?php 
    // Your query
    $args = array('post_type' => 'products');
    $loop = new WP_Query( $args );
    // Your loop
    while ( $loop->have_posts() ) : $loop->the_post();
    ?>
    <h1 class="entry-title"><?php the_title(); ?></h1>
    
    // Only print terms when the_terms is not false
    <?php if (the_terms( $post->ID, 'InternalProducts') !== false) : ?>
    <p><?php the_terms( $post->ID, 'InternalProducts', 'Category: Internal Products ', ' / ') ?></p>
    <?php endif; ?>
    
    <?php if (the_terms( $post->ID, 'ExternalProducts') !== false) : ?>
    <p><?php the_terms( $post->ID, 'ExternalProducts', 'Category: External Products ', ' / ') ?></p>
    <?php endif; ?>
    
    <?php endwhile;?>
    

    See WordPress Codex for more information.

  2. Try this code:

    <?php 
    
    //Define your custom post type name in the arguments
    
    $args = array('post_type' => 'products');
    
    //Define the loop based on arguments
    
    $loop = new WP_Query( $args );
    
    //Display the contents
    
    while ( $loop->have_posts() ) : $loop->the_post();
    ?>
    <h1 class="entry-title"><?php the_title(); ?></h1>
    <?php
    $terms = get_the_terms($post->ID, 'Your Taxonomy');
    
    foreach($terms as $term) {
         echo $term->slug;
         echo $term->name;
         echo $term->term_id;
    }
    
    endwhile;
    ?>
    

    I showed three values for your easy. Hope this will help you