0

This code uses Taxonomy Images 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:

<?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>";
?>
  • Little off-topic, but those <ul> and </ul> should be used in combination with <li>. – user26607 Mar 1 '13 at 8:43
0

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.

  • 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? – user26607 Mar 1 '13 at 11:52
0

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.

0

The Taxonomy Images plugin you're using can handle all this. The filter taxonomy-images-get-terms can take a third argument which can include an array of arguments for the underlying WP function get_terms().

Anyone not using this plugin can use a call to get_terms instead.

If you specify the current category as the parent argument, you should get all of the child categories returned. Just the children, one level, not all the deeper descendants too.

So you can use this in your theme's category.php to generate a single depth display of the current category's immediate sub-categories.

$cat_id = get_query_var('cat');
$catlist = apply_filters( 
    'taxonomy-images-get-terms', 
    '', 
    array(
        'parent' => $cat_id,
    )
);

foreach ( $catlist as $cat ) {

    echo '<a href="';
    echo esc_url( get_term_link( $cat->term_id, $cat->taxonomy ) );
    echo '">';

    echo wp_get_attachment_image( $cat->image_id, 'thumbnail' );

    echo '<p>';
    echo $cat->description; 
    echo '</p>';

    echo '</a>';
}
-2

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

  • 2
    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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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