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.

This code using taxonomy catgery 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 i should change this code to do my word

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
    
Little off-topic, but those <ul> and </ul> should be used in combination with <li>. –  Camil Staps Mar 1 '13 at 8:43
add comment

3 Answers

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

share|improve this answer
2  
Can you elaborate on your answer, explain how you would set the depth, etc –  Tom J Nowell Mar 14 at 18:17
    
-1 for this answer –  Pieter Goosen 2 days ago
add comment

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 thw sub-child category of child category. this all go in loop.

share|improve this answer
add comment

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

This:

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
    
any body ? please help? –  user28212 Mar 1 '13 at 21:11
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.