Support » Fixing WordPress » How can i list cusom categories with subcategories in one line?

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

      '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";
    echo ''.$cat->cat_name.'|'.$cat->slug.'', "\n";

    This gives me the following output:

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

    So how can i get output like this:

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


  • The topic ‘How can i list cusom categories with subcategories in one line?’ is closed to new replies.