2

I am trying to display custom category on archive page.

$term_id_array= array(1,2,4,50);

how to retrieve the term name with link for these term_ids

My custom post_type name = product taxonomy = product_cat

Note: I am using woocommerce.

Please help I am trying this code to display all product category name as a list

<?php 
$taxonomy = 'product_cat'; //Choose the taxonomy
$terms = get_terms( $taxonomy ); //Get all the terms
foreach ($terms as $term) { //Cycle through terms, one at a time
$term_id = $term->term_id; //Define the term ID
$term_link = get_term_link( $term, $taxonomy ); 
$term_name = $term->name;
echo '<p class="list-cat"><span>' . $term_id . '</span> -<span>' . $term_name . '</span></p>';  
}
?>

Out put:

1- Clothing 2- Shirt 3- Pant 4- Lady's Pant 5- T-shirt 6........ ....... 5- Sun Glass

But How can I get Name with link for this 1,2,4,50 IDs.

1 Answer 1

2

get_terms() accept an array of arguments as second parameter. One of those parameters are include

include

(integer) An array of term ids to include. Empty returns all.

You already have your selected term ids in an array, so just simply pass them to the include parameter in the array of arguments in get_terms()

$terms =  get_terms( $taxonomy, array( 'include' => $term_id_array ) );

or with short araay syntax (availble since PHP 5.4)

$terms = get_terms( $taxonomy, ['include' => $term_id_array] );
4
  • How I get them as a list? Can you please show me example code? I am new in this field.
    – Exclutips
    Commented Aug 4, 2015 at 8:52
  • 1
    I don't understand, you need to change $terms = get_terms( $taxonomy ); in your code with my code; $terms = get_terms( $taxonomy, array( 'include' => $term_id_array ) ); where you have already defined $term_id_array as $term_id_array= array(1,2,4,50); Commented Aug 4, 2015 at 9:21
  • My pleasure, glad it worked out. Enjoy ;-) Commented Aug 4, 2015 at 9:29
  • could you please help me This Question
    – Exclutips
    Commented Aug 5, 2015 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.