Support » Themes and Templates » Custom category page display subcategories with posts

  • Resolved sukient

    (@sukient)



    Hello everybody,

    I am working on a project that requiers an advanced category page that will display subcategories and posts. I need to do something like this:

    Category header

    -subcategory1 name
    –post 3 from subcategory1
    –post 2 from subcategory1
    –post 1 from subcategory1
    -subcategory2 name
    –post 3 from subcategory2
    –post 2 from subcategory2
    –post 1 from subcategory2
    ……….
    ( Only want to display the subcategory name and all the posts from that subcategory )

    Some categories will have over 10 subcateogories. All posts will be saved in subcategories, the category page will be used only for info and other stuff.

    I have searched the forums and the net but did not find any good info on how to do this or something to start with.

    Looking forward for some help.

    Best Regards,
    Julian

Viewing 10 replies - 1 through 10 (of 10 total)
  • i’ve just posted the exact same request in another topic! here

    I know how it could work (as i’ve detailed) but i’m not sure if it’s the correct way of doing it. Technically using the same template page and duplicating some code within it along with incrementing the category variable passed into the page would work. I’m just unsure how to do it in wordpress land.

    <?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('cat='.$child->term_id);
        while(have_posts()): the_post(); $do_not_duplicate = $post->ID;
         //post code stuff here;
        endwhile;
        wp_reset_query();
    }
    endif;
    ?>

    Try this logic.

    great thanks! will give that a go today!

    thanks chinmoy29, logical it should work like this. No when I implement it in the category page I get the following error:

    Parse error: syntax error, unexpected T_DOUBLE_ARROW in /***/***/public_html/***/wp-content/themes/k2/category.php on line 11

    and on line 11 is the following code:\

    $childCats = get_categories( ‘child_of’ => $parentCatID );

    check the syntax of ‘get_categories()’

    http://codex.wordpress.org/Function_Reference/get_categories

    $childCats = get_categories( 'child_of='.$parentCatID );

    or

    $childCats = get_categories( array('child_of' => $parentCatID) );

    btw:
    child_of will also get you the sus-sub-and-so-on categories
    parent will only give the direct sub cats

    Thank you alchymyth,

    I managed to make it work as I wanted. Thank you again. Here is the code that works.

    <?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;
    ?>

    Thanks again for helping me.

    Best Regards,
    Julian

    where should i put the above code..

    very sorry i new in wp.is it in side bar.php??

    Nevermind…

    This is just what I want – BUT it only shows sub categories and omits all posts in the parent category. Would anyone have a tweak to show the top level as well?

    how can I use the variable $do_not_duplicate = $post-> ID in this loop for not having duplicate posts?

    Best Regards

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Custom category page display subcategories with posts’ is closed to new replies.