1

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>";
?>

1 Answer 1

1

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().

1
  • 1
    You are welcome...!!! Dont forget to accept answer... :) Commented Mar 4, 2013 at 9:13

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.