• 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 to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • 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.

    Thread Starter mrtargets

    (@mrtargets)

    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 6 years, 10 months ago by mrtargets.

    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

    (@mrtargets)

    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 6 years, 10 months ago by mrtargets.
    • This reply was modified 6 years, 10 months ago by mrtargets.
    Moderator bcworkz

    (@bcworkz)

    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

    (@mrtargets)

    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

    (@bcworkz)

    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

    (@mrtargets)

    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.