WordPress.org

Ready to get started?Download WordPress

Forums

How can i list cusom categories with subcategories in one line? (1 post)

  1. Metuza
    Member
    Posted 1 year ago #

    I have an script which i use to copy categories into new categories with another taxonomy, it works great with 20+ categories with 2-3 levels of subcategories, BUT if i copy 2-300 categories with a big mix of id's in regards to how and when they orginally was added, and some categories is 10-12 levels deep then it does not add the copies hierarchial correctly. It does copy all categories but i have to edit parent of 10-12 always.

    This is the code i use today:

    $args=array(
      'taxonomy' => $taxonomy,
      'hierarchical' => 0,
      'hide_empty' => 0,
      'orderby' => 'name',
      'order' => 'asc'
      );
    $categories = get_categories($args);
    foreach ($categories as $cat) {
    if ($cat->category_parent != 0) {
    $term = get_term($cat->category_parent, $taxonomy);
    echo ''.$term->name.'|'.$term->slug.'*'.$cat->cat_name.'|'.$cat->slug.'', "\n";
    }else{
    echo ''.$cat->cat_name.'|'.$cat->slug.'', "\n";
    }
    }

    This gives me the following output:
    Cat1|cat1*Cat2|cat2

    Problem is that if Cat1 has 2 or more level1 childs then the second loose its parent (Cat1) and get listed like this:
    Cat1-2|cat1-2

    So how can i get output like this:
    Cat1|cat1*Cat2|cat2
    Cat1|cat1*Cat1-2|cat1-2

    And ofcourse unlimited depth, so i get all child levels listed.

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic