Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

My code uses a taxonomy category image plugin to display images for categories. It also displays subcategories of the main category in the parent category page (category.php). I want to display only one level of subcategories 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, they should see only the monitor subcategory link. Clicking on monitor, they should see the samsung category link, and when they click on samsung, lcd should display.

How I should change my code to achieve this?

My code:

<?php
$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
add comment

1 Answer

Change the second line of your code to this:

$catlist = get_categories( 'hide_empty=0&parent=' . $cat_id );

Instead of child_of parameter, use the parent parameter of the get_categories() function; this will display the direct descendants (i.e. children only) of the category and not grandchildren of the category.

For more information visit the Codex page for get_categories().

share|improve this answer
    
Thank you . it is working well now –  user28212 Mar 4 '13 at 9:10
    
You are welcome...!!! Dont forget to accept answer... :) –  Vinod Dalvi Mar 4 '13 at 9:13
add comment

Your Answer

 
discard

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.