WordPress.org

Ready to get started?Download WordPress

Forums

Main Category - Show all subs and their posts (12 posts)

  1. mikeboy3
    Member
    Posted 4 years ago #

    Hello,
    I hope this is a simple question, is it possible to have a loop that displays the parent category, and all posts in it, but organized by sub-categories?

    This page is supposed to work as a catalog, so, it will have several Main Categories that hold many products in sub-categories. The idea is to display each Main Category and show all posts, something like:

    Main category
           Sub-Cat 1
                  Post 1
                  Post 2
                  Post 3
           Sub-Cat 2
                  Post 1
                  Post 2
    etc...

    Should that loop be in the category.php template? I thought about it, and all I can think of is a separate category-slug.php file for each main category, but there has to be a better way.

    Thank you very much for your help!

  2. mikeboy3
    Member
    Posted 4 years ago #

    Please? any ideas?

  3. MichaelH
    Member
    Posted 4 years ago #

    Or put it in category.php and use a conditional tag such as is_category(array(5,7,2,12))) to 'wrap around' your code that display the category/sub-cat/post logic.

  4. mikeboy3
    Member
    Posted 4 years ago #

    I found this post: http://wordpress.org/support/topic/388420?replies=5#post-1529178

    which features this code for category.php, actualy, it's code you wrote about a month ago and Calicosun edited:

    <?php
    //get terms (e.g. categories or post tags), then display all posts in each retrieved term
    $taxonomy = 'category';//  e.g. post_tag, category
    $param_type = 'category__in'; //  e.g. tag__in, category__in
    $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'child_of' => 16
    );
    $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
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
    
          echo '<li><a href="' . get_category_link( $term->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name. '</a> ';
    
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <ul><li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li></ul>
           <?php
                 endwhile;
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    This almost works, now the issue is how to have it recognize the category it's in instead of specifying it on the top as 'child_of' => 16

    Thanks for your help!

  5. MichaelH
    Member
    Posted 4 years ago #

    You mean like this:

    <?php
    if (is_category( )) {
      $current_cat = get_query_var('cat');
    }
    ?>
  6. mikeboy3
    Member
    Posted 4 years ago #

    I guess, I'm not good with php, how could I implement that into the code above?

    Edit: I figured it out, thank you very much man!
    Here is the final code that worked

    <?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
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
    
          echo '<h4>' . $term->name. '</h4> '; // Sub-Category Title
    
          while ($my_query->have_posts()) : $my_query->the_post(); ?>// Post Contents
    
    Post content here
    	<?php the_title(); ?>
    	<?php the_excerpt(); ?>
    
           <?php
                 endwhile;
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  7. MichaelH
    Member
    Posted 4 years ago #

    If I understand what you want:

    'child_of' => get_query_var('cat')
  8. mikeboy3
    Member
    Posted 4 years ago #

    Exactly! thanks again!

  9. mikeboy3
    Member
    Posted 4 years ago #

    Ok, I'm back, I realized that this code only shows categories that have sub-categories, if I try to open a sub-category and display all posts on that sub-category, the page loads blank.

    What can I do about that?

    I don't know much php, so I would think something like:

    if (has_children()) {
    		load the code above
    	}
    else {
    		load the regular category.php code
    }

    (I know, I am not a coder so please don't laugh too hard)

    Thanks in advance!

  10. mikeboy3
    Member
    Posted 4 years ago #

    any ideas?

  11. mikeboy3
    Member
    Posted 4 years ago #

    please help, anyone?

  12. mikeboy3
    Member
    Posted 4 years ago #

    please, it's urgent, it's a real problem that I need to fix and I have no Idea how

Topic Closed

This topic has been closed to new replies.

About this Topic