Title: Category array
Last modified: January 24, 2018

---

# Category array

 *  [mrtargets](https://wordpress.org/support/users/mrtargets/)
 * (@mrtargets)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/category-array/)
 * Hello everyone!
 * I have created a page to showcase all categories and their respective posts heading
   and selected image. Now I want to be able to add subcategories to them and these
   subcategories should **not ** appear on this page. I’ve searched everywhere and
   tried all solutions I can think of but nothing works! Is there anyone who can
   help me shine some light on this?
 * Here is the code I’m using today (without styling):
 *     ```
       <?php
       $cats = get_categories(array('orderby' => 'id', 'order' => 'ASC', 'parent' => '3'));
       foreach ($cats as $cat) {
       $cat_id= $cat->term_id;
       echo "<br clear='all' /><h3>".$cat->name."</h3>";
       query_posts("cat=$cat_id&posts_per_page=100&order=asc&orderby=title");
       if (have_posts()) : while (have_posts()) : the_post(); 
       ?>
       <?php ?>
       <a href="<?php the_permalink();?>">
       <?php if ( has_post_thumbnail() ) {the_post_thumbnail('thumbnail');} ?>
       <?php the_title(); ?>
       </a>
       <?php endwhile; endif; ?>
       <?php } ?>
       ```
   
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fcategory-array%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 9 replies - 1 through 9 (of 9 total)

 *  [Vignesh Pichamani](https://wordpress.org/support/users/viky081/)
 * (@viky081)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/category-array/#post-9898181)
 * You have to pass the parent category id to get the child of corresponding categories.
 *     ```
       $categories=get_categories(
           array( 'parent' => $cat_id )
       );
       ```
   
 * Thank you,
    Vignesh Pichamani.
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/category-array/#post-9900229)
 * related?
    [https://wordpress.stackexchange.com/questions/127872/get-only-the-top-level-categories-using-get-categories-without-foreach-loop](https://wordpress.stackexchange.com/questions/127872/get-only-the-top-level-categories-using-get-categories-without-foreach-loop)
 *  Thread Starter [mrtargets](https://wordpress.org/support/users/mrtargets/)
 * (@mrtargets)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/category-array/#post-9900473)
 * **Hi, thank you for your answers.**
 * I have already tested that solution, but unfortunately it does not help me.
    
   Will try to explain the structure of my categories a bit better.
 * **The page “Categories” displays;**
 * – Category 1 (Title)
    — Post 1 (Title & image) — Post 2 (Title & image)
 * – Category 2 (Title)
    — Post 1 (Title & image) — Post 2 (Title & image)
 * **Current situation that i need to fix look like this:**
 * – Category 1 (Title)
    — Post 1 (Title & image) —- Post 1.1 (Child to “Post 1”–
   _Should not be listed_) — Post 2 (Title & image)
 * – Category 2 (Title)
    — Post 1 (Title & image) —- Post 1.1 (Child to “Post 1”–
   _Should not be listed_) — Post 2 (Title & image)
    -  This reply was modified 8 years, 3 months ago by [mrtargets](https://wordpress.org/support/users/mrtargets/).
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/category-array/#post-9904188)
 * by default, posts do not have a hierarchical structure, as for example static
   pages would have.
 * how do you define your posts as ‘Child to’ another post?
 * what theme are you using?
 *  Thread Starter [mrtargets](https://wordpress.org/support/users/mrtargets/)
 * (@mrtargets)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/category-array/#post-9905075)
 * Thanks for your reply.
    It was me who had missed a little in my explanation, 
   sorry for that.
 * **Here is an updated version of the structure:**
 * – Category 1 (Title)
    —- Category 1.1 (Posts in this child category should not
   be listed on page) — Post 1 (Title & image) — Post 2 (Title & image)
 * – Category 2 (Title)
    —- Category 2.1 (Posts in this child category should not
   be listed on page) — Post 1 (Title & image) — Post 2 (Title & image)
 * It is by the way a theme I have written myself, no pre-written framework.
    -  This reply was modified 8 years, 3 months ago by [mrtargets](https://wordpress.org/support/users/mrtargets/).
    -  This reply was modified 8 years, 3 months ago by [mrtargets](https://wordpress.org/support/users/mrtargets/).
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/category-array/#post-9908083)
 * How does Michael’s SE link not help you? get_terms() with a parent argument of
   0 will only return top level terms, which appears to be exactly what you want.
 *  Thread Starter [mrtargets](https://wordpress.org/support/users/mrtargets/)
 * (@mrtargets)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/category-array/#post-9912375)
 * Hello again
 * I do not know how to make me clearer but when I use “parent => 0” I get the following
   structure:
 * – Main category
    — All posts in the system, no matter what category they are 
   in – parent or child.
 * The way I want the structure to look like is as follows:
 * – Main category – Should not show
    — **Subcategory 1 – Print out name och category**—
   Post in Subcategory 1 – Print out title and image —- Child to Subcategory 1 –
   NOT visible —– Posts in children to subcategory 1 – not shown — **Subcategory
   2 – Print out name och category** — Post in Subcategory 2 – Print out title and
   image —- Child to Subcategory 2 – NOT visible —– Posts in children to subcategory
   2 – not shown
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/category-array/#post-9918732)
 * Well, it wasn’t clear before that you did not want the main category. Thank you
   for clarifying.
 * If you do not want the main category, do not use `'parent'=> 0`, instead of 0,
   use the ID of the subcategory’s parent, the “Main category” ID. For each sub-
   category returned, use get_posts() using the ‘cat’ argument to get posts with
   the respective term assigned.
 *  Thread Starter [mrtargets](https://wordpress.org/support/users/mrtargets/)
 * (@mrtargets)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/category-array/#post-9925196)
 * Hello again and thank you very much to all those who thought about this issue.
   
   I will sit down and rewrite the code from start to finish and we’ll see where
   I land. Believe that I may have thought wrong from the beginning: P

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Category array’ is closed to new replies.

## Tags

 * [categories](https://wordpress.org/support/topic-tag/categories/)
 * [levels](https://wordpress.org/support/topic-tag/levels/)
 * [parents](https://wordpress.org/support/topic-tag/parents/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 9 replies
 * 4 participants
 * Last reply from: [mrtargets](https://wordpress.org/support/users/mrtargets/)
 * Last activity: [8 years, 3 months ago](https://wordpress.org/support/topic/category-array/#post-9925196)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
