Forums

List posts in category AND subcategories (15 posts)

  1. rickdavidson
    Member
    Posted 1 year ago #

    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;
        ?>
  2. rbohall
    Member
    Posted 1 year ago #

    Any luck on this? I've been trying for a while now to do the same thing, without success.

  3. alchymyth
    The Sweeper
    Posted 1 year ago #

    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)

  4. rickdavidson
    Member
    Posted 1 year ago #

    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...

  5. alchymyth
    The Sweeper
    Posted 1 year ago #

    this might work:

    http://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?

  6. rickdavidson
    Member
    Posted 1 year ago #

    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 >

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

    The 'cat=' query doing that? Sorry I'm no expert :(

  7. bunny_ch
    Member
    Posted 11 months ago #

    Hola,

    Did you achieve what you wanted to achieve? I'm not sure what you want to list ...

  8. rickdavidson
    Member
    Posted 10 months ago #

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

    If you look at the list -
    http://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.

  9. bunny_ch
    Member
    Posted 10 months ago #

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

    MAIN CAT
    SUB CAT
    SUB CAT

    MAIN CAT
    SUB CAT
    SUB CAT

    etc....

  10. rickdavidson
    Member
    Posted 10 months ago #

    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
    :)

  11. alchymyth
    The Sweeper
    Posted 10 months ago #

    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?

  12. rickdavidson
    Member
    Posted 10 months ago #

    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]

  13. rickdavidson
    Member
    Posted 9 months ago #

    Anyone? No-one? :)

  14. uhmaster2000
    Member
    Posted 7 months ago #

    wp_list_categories will do the trick.

    http://codex.wordpress.org/Template_Tags/wp_list_categories

  15. suzybaroud
    Member
    Posted 5 months ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic