WordPress.org

Forums

list categories (9 posts)

  1. JohnAnderson10
    Member
    Posted 7 years ago #

    Hey guys is there a way to have a page(php) that would list all the categories like

    Main_Category
    sub1,sub2,sub3

    Main_Category2
    sub1,sub2,sub3

    If you know where i can get that info I would really appricate it

  2. JohnAnderson10
    Member
    Posted 7 years ago #

    Hey i came up with this and its almost there but it list the sub categories aswell is there any way that it dosent list the sub categories

    <?php foreach((get_categories('')) as $cat)
    {
    echo 'cat_name . '">'.$cat->cat_name.'
    ';
    $categories= get_categories('child_of='.$cat->cat_ID);
    foreach ($categories as $cat2) {
    $option .= $cat2->cat_name;
    $option .= ',';
    $option .= '';
    echo $option;
    }
    echo '

    ';
    }
    ?>

  3. TrishaM
    Member
    Posted 7 years ago #

    Your answer conflicts with your question.

    In your first post you are asking for a way to display main categories with sub categories below it.

    In your second post you are asking how to not display sub categories.

    Which one do you want?

  4. JohnAnderson10
    Member
    Posted 7 years ago #

    lol no ya in the second post i made some code that would show it the way i want but once its finished with listing the sub cat its then starts to list the sub-cat as main-categories like this

    MAIN_CAT
    sub,sub,sub

    SUB_CAT
    SUB_CAT
    SUB_CAT
    I belive the frist loop is reading the subcategories as well I only want the top level categories in the frist loop as I will do a second loop to get the sub categories

    that my problem right now

  5. Ivovic
    Member
    Posted 7 years ago #

    do this test inside your first foreach loop....

    This tests to see how many parents your category has - if the subcategory is a grandchild, it'll just be skipped in this loop.

    $parents = explode('|',get_category_parents($cat2,false,'|'));
    if ($parents[2] == "") {
      (do what you want here)
    }
  6. JohnAnderson10
    Member
    Posted 7 years ago #

    Ha it works thanks so mu i just did a few tweaks to the code you gave m its running just fine

    here is the code if anyone need its

    foreach((get_categories('')) as $cat)
    {
    $parents = explode('|',get_category_parents($cat,false,'|'));
    if ($parents[1] == "") {
     echo '<b>' . $cat->cat_name . '</b>'.$cat->cat_name.'<br /> ';
     $categories=  get_categories('child_of='.$cat->cat_ID);
     foreach ($categories as $cat2) {
    	$option = $cat2->cat_name;
    	$option .= ',';
    	echo $option;
      }
      echo '<br /><br />';
     }
    }
  7. Ivovic
    Member
    Posted 7 years ago #

    Oh... sorry, I should have edited what I posted a little better - for me it's [2] because I'm listing subchildren of a particular category.

    Glad you got it worked out!

  8. JohnAnderson10
    Member
    Posted 7 years ago #

    AHHHHHH it works but it wontr show more then 1 sub-cat

  9. Ivovic
    Member
    Posted 7 years ago #

    isnt that exactly what you wanted? you want it to show one level of subcat, right?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.