I am trying to get print names of custom taxonomy that i have created for products.
function create_product_taxonomies()
{
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x('product_categories', 'taxonomy general name'),
'singular_name' => _x('Product', 'taxonomy singular name'),
'search_items' => __('Search Product Category'),
'all_items' => __('All Product Categorie(s)'),
'parent_item' => __('Parent Product Category'),
'parent_item_colon' => __('Parent Product Category:'),
'edit_item' => __('Edit Product Category'),
'update_item' => __('Update Product Category'),
'add_new_item' => __('Add New'),
'new_item_name' => __('New Product Name'),
'menu_name' => __('Product Categories'),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'product_categories', 'with_front' => true));
register_taxonomy('product_categories', array('products'), $args);
i have added data through the wordpress admin panel. Now i want to Display the names of the categories in product.php file.
function getLatestProducts()
{
$args = array(
'post_status' => 'publish',
'post_type' => 'products',
'posts_per_page' => 12,
'order' => 'ASC'
);
$result = '<div class="col-sm-3">';
$loop = new WP_Query($args);
$i=0;
while ($loop->have_posts())
{
$loop->the_post();
$clink=get_permalink($post->ID);
$desc=get_the_excerpt();
$categories = get_terms( 'product_categories');
$desc = strip_tags(str_replace(array("<p>", "</p>"), "", $desc));
$the_imgurl = get_post_custom_values('_cus_n__image');
$theimage=$the_imgurl[0];
$the_locurl = get_post_custom_values('_cus_n__location');
$theloc=$the_locurl[0];
echo $categories;
$result .='<div class="product-warp">';
$result .='<div class="product"> <a href="#"><img src="/wp-content/themes/cake/images/pro1.jpg" title="" alt=""></a> </div>';
$result .='<div class="product-name">';
$result .='<h5><a href="#">'.$categories.'</a></h5>';
$result .='</div>';
$result .='</div>';
$i++;
}
$result .= '</div>';
if($i > 0){
return $result;
} else {
return "";
}
}
it is just printing this arrayarrayarrayarrayarrayarray
Ok bro you can use get_terms function for this purpose. Here is the example:
I only give you an example. You can paste my code where you want.
Now use the WordPress Taxonomy Template for that when user click on one of your category and next page show all the related products of clicked category and also you must read this.
If you read
taxonomy Template
link then we go to next step.Now you create a file
taxonomy-product_categories.php
in your theme root folder.This create template for you taxonomy.
Now in this file here is the complete code:
Once again I told you that I give you only a code you can merge this code in your theme.
Hope this'll help you.