• Hoping there is a guru who could tweak this code or provide something new.

    I need to break down and list all posts in a category AND it’s subcategories. The following code only lists posts in the sub cats, so any post in the main category is skipped. So close though. Help?

    [please mark your code using the ‘code’ button or backticks;
    next time, please, for any code of more then 10 lines, use the pastebin]

    <?php
        $parentCatName = single_cat_title('',false);
        $parentCatID = get_cat_ID($parentCatName);
        $childCats = get_categories( 'child_of='.$parentCatID );
        if(is_array($childCats)):
        foreach($childCats as $child){ ?>
        <h2><?php echo $child->name; ?></h2>
        <?php query_posts('cat='.$child->term_id);
        while(have_posts()): the_post(); $do_not_duplicate = $post->ID; ?>
        <!-- POST CODE -->
    
        <!-- END POST CODE -->
        <?php
        endwhile;
        wp_reset_query();
        }
        endif;
        ?>
Viewing 14 replies - 1 through 14 (of 14 total)
  • Any luck on this? I’ve been trying for a while now to do the same thing, without success.

    unless you need to have the posts ordered by category with the category name on top –
    the ‘normal’ query should do it:

    <?php
        $parentCatID = get_cat_ID(single_cat_title('',false));
            <?php query_posts('cat='.$parentCatID);
        while(have_posts()): the_post(); $do_not_duplicate = $post->ID; ?>
        <!-- POST CODE -->
    
        <!-- END POST CODE -->
        <?php
        endwhile;
        wp_reset_query();
            ?>

    (untested)

    Thread Starter rickdavidson

    (@rickdavidson)

    Hey Alchymyth, thanks but yes that’s what I’m after, the sub-categories listed, but including the top level as well ?? Trickier than I thought…

    this might work:

    https://pastebin.com/4rWrYqzK

    open to dealing with details:
    – the edit simply integrates the parent category into the array;
    – do you need to mark the parent category (not done in the current version)?
    – are you planning to use the ‘$do_not_duplicate’ variable?
    – are you aware that ‘cat= ‘ will also query posts of child categories?

    Thread Starter rickdavidson

    (@rickdavidson)

    Hey Alchy,

    Thank you so much for your help. Sorry, I’ve only just had a chance to look at this.

    Very close! My only problem now is that the child categories are listed under the main section, and then again under their child section – see >

    https://www.kawikadesign.com/domains/tubularequipment.co.nz/Catalogue/01-walking-frames/

    The ‘cat=’ query doing that? Sorry I’m no expert ??

    Hola,

    Did you achieve what you wanted to achieve? I’m not sure what you want to list …

    Thread Starter rickdavidson

    (@rickdavidson)

    Bunny hi! Sorry I never got your post. No I am STILL trying to fix.

    If you look at the list –
    https://www.kawikadesign.com/domains/tubularequipment.co.nz/Catalogue/01-walking-frames/

    the first four items are actually subcategories. They display again under their subcategories at the bottom. I really just need these to display only under the subcats. The rest is perfect.

    SO the final version would be:
    List all items in the main category
    Then list all the items in the subcategories under their headings.

    Would LOVE if I could get help here.

    cool… so what you want to do is this?:

    MAIN CAT
    SUB CAT
    SUB CAT

    MAIN CAT
    SUB CAT
    SUB CAT

    etc….

    Thread Starter rickdavidson

    (@rickdavidson)

    Hmm, the link example is pretty close, but if you take a look, Rehab forearm 1 is a subcategory item under REHAB FOREARM. So it shouldn’t be listing at the top under the main category as well. So I need something like:

    MAIN CATEGORY HEADING
    list main category items only
    SUB CATEGORY HEADING
    list sub category items only
    SUB CATEGORY HEADING
    list sub category items only
    ??

    it looks as if you need to break this into several loops;

    first: top category posts loop only (using ‘category__in’ parameter in the query; possibly collect the post id for ‘do-not-dup’ purpose;

    second:
    get all subcategories;
    foreach loop through each of them;
    post loop with the ‘category__in’ and ‘post__not_in’ parameters in the query; again, possibly collect the post id for ‘do-not-dup’ purpose;
    endforeach;

    can you paste the full code of your template as a starting point for any suggestions?

    Thread Starter rickdavidson

    (@rickdavidson)

    Sure thing alchymyth, thank you. I used the code you posted earlier, i think you’re probably right on the repeating loops. Here’s the full template ??

    [Code moderated as per the Forum Rules. Please use the pastebin]

    Thread Starter rickdavidson

    (@rickdavidson)

    Anyone? No-one? ??

    Dear All,

    I am new to wordpress and I want to add a category and subcategories to my site. I am going to appearance and then to menu, but I don’t know what to do after that, any advice?

    Suzy

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘List posts in category AND subcategories’ is closed to new replies.