WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This code uses taxonomy category image plugin to display images for categories. This code display subcategories of main category in parent category page (category.php), But I want to display only one level of subcategory in the page for example

hardware (parent category)

  • monitor (first level subcategory of harware)

    • samsung (second level subcategory)

      • lcd (third level subcategory )

When the user clicks on the hardware link, he should see only monitor subcategory link and in second time when he clicks on the monitor, he should see the samsung category link, and when he click on the samsung the subcategory LCD will displays. How should I change this code to do my word

My code:

$cat_id = get_query_var('cat');
$catlist = get_categories('hide_empty=0&child_of=' . $cat_id);
echo "<ul>";

foreach($catlist as $categories_item)
echo '<h1><a href="' . get_category_link( $categories_item->term_id ) . '" title="' . sprintf( __( "View all products in %s" ), $categories_item->name ) . '" ' . '>' . $categories_item->name.'</a> </h1> ';

echo '<div class="categoryoverview clearfix">';
    $terms = apply_filters( 'taxonomy-images-get-terms', '' );
    if ( ! empty( $terms ) ) {

      foreach( (array) $terms as $term ) {
        if($term->term_id == $categories_item->term_id) {
           print '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, 'thumbnail' );
           echo '</a>';
    echo '<p>'. $categories_item->description; echo '</p>';
echo '</div>';
echo "</ul>";
share|improve this question
Little off-topic, but those <ul> and </ul> should be used in combination with <li>. – Camil Staps Mar 1 '13 at 8:43

I'm going to guess here. I assume you are using the Taxonomy Images plugin?


if($term->term_id == $categories_item->term_id) {

Can be changed to:

if($term->term_id == $categories_item->term_id && $term->parent == $categories_item->cat_ID) {

Hereby, you check if the parent ID of the term is the same as the ID of the categories_item.

share|improve this answer
yes i am usng taxonomy images plugin, but your code did not works any idea again? – user28212 Mar 1 '13 at 10:19
Okay, so does this work? – Camil Staps Mar 1 '13 at 11:52

The parent category (hardware) you should display this category in any page by getting all the parent categoty using

$parent_category = get_categories(array('orderby' => 'name','parent' => 0));
foreach($categories as $category) {
    echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';

When you click on the category then it will go to category.php page. In category.php page you will get all the category whose parent is hardware.

get_categories(array('parent' => 'hardware_category_id/its_sub_category_id',
                     'hide_empty' => 1,));

Then You should display all the child category of hardware. display these category in <a></a> tag as we display earlier. Whenever you click on child category link then it will go again to category.php page ant it will the take the category id as child category id and it will show the sub-child category of child category. this all go in loop.

share|improve this answer

You could use wp_list_categories instead. It has an ability to set depth.

share|improve this answer
Can you elaborate on your answer, explain how you would set the depth, etc – Tom J Nowell Mar 14 '14 at 18:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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