• Greetings, I have been working on a site that requires a very complex category display, it’s a product catalog and it is organized in categories and sub-categories.

    The category pages have to display all posts organized by sub-category, something like:

    Main Category
    Sub-Cat1
    Post1
    Post2
    Sub-Cat2
    Post3
    Post4

    So far, thanks to help from guys at this forum, we have managed to make that work by using the following code:

    <?php
    //get terms (e.g. categories or post tags), then display all posts in each retrieved term
    $taxonomy = 'category';//  e.g. post_tag, category
    $current_cat = get_query_var('cat');
    $param_type = 'category__in'; //  e.g. tag__in, category__in
    $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'child_of' => get_query_var('cat')
    );
    $terms = get_terms($taxonomy,$term_args);
    if ($terms) {
      foreach( $terms as $term ) {
        $args=array(
          "$param_type" => array($term->term_id),
          'post_type' => 'post',
          'post_status' => 'publish',
          'showposts' => -1,
          'caller_get_posts'=> 1,
          'orderby'=> 'title',
          'order'=> 'ASC',
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
    
          echo '<div class="gs12col" id="cat_title" >
    <h4>' . $term->name. '</h4>
    </div> '; 
    
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
    	<div class="gs480" id="postclass">
    	<div class="gs2col" id="thumbnail">
    	<a href="<?php the_permalink() ?>"><?php the_post_thumbnail( array(140,140) ); ?></a>
    	</div>
    	<div class="gs4col" id="postcontent">
    	<a href="<?php the_permalink() ?>"><h5><?php the_title(); ?></h5></a>
    	<?php the_excerpt(); ?>
    	</div>
    	</div>
    
           <?php
                 endwhile;
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Now, I need to display an image thumbnail and I want to use this code:

    <?php
    foreach((get_the_category()) as $category) {
        echo '<img src="https://example.com/images/' . $category->cat_ID . '.jpg" alt="' . $category->cat_name . '" />';
    }
    ?>

    Found here at the CODEX

    However, this does not work properly, since it does not identify the correct cat_ID, it uses one of the sub-category’s ID instead of the main category.

    I think the solution is to combine both codes so that as it identifies the main category to get all sub-cats, it can also define the image’s cat_ID as the main category. I don’t know how to do this, I’ve been trying but no luck.

    Any ideas?

    Thanks in advance! I know it was a long post, thanks for taking the time to help!

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Complex category display with thumbnail’ is closed to new replies.