Forums

list categories and subcategories with descriptions in nested list (14 posts)

  1. akallen
    Member
    Posted 1 year ago #

    I am trying to use get_categories to display a hierarchical list of categories and descriptions. Instead of diplaying subcategories in a nested list under the parent, I get one long list. Here is my code:

    <ul>
     <?php $categories = get_categories('title_li=&hide_empty=0&parent=31');
           foreach ($categories as $category) {
           echo "<li><a href=\"" . get_category_link( $cat->term_id ) . "\">" . $cat->cat_name . "<div class=\"description\">". $cat->description ."</div></a></li>";
           } ?>
    </ul>

    I'm not a php programmer, so I'm not sure how to modify this to show hierarchy. Any help would be great.

  2. alchymyth
    The Sweeper
    Posted 1 year ago #

    link to your site?

    get_categories() gets a flat list of the categories;
    to get it to show hierarchy, in the first step, you need to get the top level categories only;
    and then run foreach loops in which you only get the direct category children of each category; you can do this using the 'parent' parameter;
    and so on.

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

  3. akallen
    Member
    Posted 1 year ago #

    Here is the link: http://www.allenwebcreation.com/public_html/wp/research-our-collections/

    I used the following code to show only the parent categories.

    <?php
    $args=array(
      'orderby' => 'name',
      'order' => 'ASC'
      );
    $categories=get_categories('title_li=&hide_empty=0&parent=31');
      foreach($categories as $category) {
        echo '<p><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
        echo '<p>'. $category->description . '</p>';}
    ?>

    Can you tell me how to modify the code to list the subcategories? That will allow me to add a style to indent them or list them.

    Thanks!

  4. akallen
    Member
    Posted 1 year ago #

  5. akallen
    Member
    Posted 1 year ago #

    Sorry, I forgot to close the link.

  6. alchymyth
    The Sweeper
    Posted 1 year ago #

    i had a bit of difficulties to convert that code to show descriptions instead of posts; but here is a rework; slightly different to the other one (the first part of the code is now also integrated into the function; there is still a space to add your 'exclude' ids):
    it also contains the formatting that was showing on your site; and the category titles are linked.

    http://pastebin.com/6WNG2N33

  7. akallen
    Member
    Posted 1 year ago #

    Your code works as long as there are posts within a category. That will work in the future once I hand the site over to the client to fill it up. Is there any way to force empty cats to display?

    Thanks very much for your help!

  8. alchymyth
    The Sweeper
    Posted 1 year ago #

    my mistake -
    instead of 'hide_empty=false'

    it should be 'hide_empty=0'

    in this area:

    if($cat == '') :
    //add any parameter for get_categories() into the code in the next line//
         foreach( get_categories('hide_empty=0') as $category ) :
         if( !$cat->parent ) { $next[] = $category; }
    	 endforeach; 
    
       else:
         $next = get_categories('hide_empty=0&parent=' . $cat);
       endif;

    hope this helps ;-)

  9. akallen
    Member
    Posted 1 year ago #

    Now all of the cats show. Is there a way to put the children in a nested list under the parent?

    Thanks!

  10. alchymyth
    The Sweeper
    Posted 1 year ago #

    Is there a way to put the children in a nested list under the parent?

    that is what the code does.

    if you have edited anything, please re-paste the code into a pastebin...

    which of the shown categories are children of which other one?

  11. akallen
    Member
    Posted 1 year ago #

    Sorry, it looks like a flat list but I need to style it. Pastebin is down so I can't send you what I have. The categories are (children are in ()):
    Archive of Amer. Dem.
    Arcive American Veterans...(Bob Womack.., Veterans Hist. Proj.)
    Archive Local Hist..
    Archive Midd. TN State U. (Papers of Alum..., Records of MTSU)
    Archive Midd TN Women
    Margaret Lindsley...

    It displays correctly but then repeats some of the cats. Any ideas?

    Thanks again!

  12. akallen
    Member
    Posted 1 year ago #

    Well, I modified the code on your website for a list of all cats with linked post titles to remove the posts and link the categories. Here is the page: http://www.allenwebcreation.com/public_html/wp/american-revolution/. As you can see, it is working correctly.

    I also created a page that includes posts using your code.
    http://www.allenwebcreation.com/public_html/wp/research-our-collections2/

    I would send you a link to pastebin, but it seems to be down today.

    I really appreciate all of your help.

  13. alchymyth
    The Sweeper
    Posted 1 year ago #

    where are the remaining problems - if any?

    i see, the hierarchy is there in both links;
    there are no repeats.

    the 'test post' is showing in 'research-our-collections2' in all subcategories - that is rather unexpected - however, without seeing the code, i cant't tell why.

    hope the pastebin is working again soon.

  14. akallen
    Member
    Posted 1 year ago #

    At this point, there aren't any problems. I put a test post in each category.

    Thanks again!

Topic Closed

This topic has been closed to new replies.

About this Topic