Hopefully an easy fix for someone!
I have a custom post type, named ‘products’.
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:
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:
See WordPress Codex for more information.
Try this code:
I showed three values for your easy. Hope this will help you