php - WordPress Taxonomy get category -
hopefully easy fix someone!
i have custom post type, named 'products'.
using code, getting name of each 'product':
<?php //define custom post type name in arguments $args = array('post_type' => 'products'); //define loop based on arguments $loop = new wp_query( $args ); //display contents while ( $loop->have_posts() ) : $loop->the_post(); ?> <h1 class="entry-title"><?php the_title(); ?></h1> <?php endwhile;?>
each of these products inside category. want not the_title, category.
i tried using <?php the_category(); ?>
no luck.
anyone got clue?
edit:
sorry, have these created inside 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 , internal products categories. inside these, have categories have been made on wordpress backend.
this how looks when select product:
this bit difficult because attached 2 different taxonomies post type 'products'. since taxonomy hierarchical have been easier have put in 1 taxonomy 'internal prodcuts' , 'external products' on top level. in case have check both of them - assume 'products' can't 'internal' , 'external' @ same time:
<?php // query $args = array('post_type' => 'products'); $loop = new wp_query( $args ); // loop while ( $loop->have_posts() ) : $loop->the_post(); ?> <h1 class="entry-title"><?php the_title(); ?></h1> // print terms when the_terms 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 more information.
Comments
Post a Comment