0
votes

I found this code:

<?php 
$args = array(
  'orderby' => 'id',
  'hide_empty'=> 0,
  'child_of' => 2,
'depth' => 5,
);
$categories = get_categories($args);
 foreach ($categories as $cat) {
    echo '<div>';
    echo '<h1>'.$cat->name.'<img src="'.$cat->term_icon.'" alt=""  class="alignleft"/>'.'<br />'.'<span class="solutions">'.$cat->description.'</span>'.'</h1>';
    //echo '<br />';     
 $args3= array("orderby"=>'name', "category" => $cat->cat_ID, 'depth' => 5); // Get Post from each Sub-Category
    $posts_in_category = get_posts($args3);
    foreach($posts_in_category as $current_post) {
        echo '<span>';
        ?>
        <li><h1><a href="<?=$current_post->guid;?>"><?=$current_post->post_title;?></a></li></h1>
        <?php
        echo '</span>';
    }
    echo '</div>';
}
?>

This lists all categories and posts from a category. But i want it from all categories. But when i fill in 'child_of' => 2 it lists everything, but it does not get formatted well. grandchilds have the same hierarchical status as the children.

what i want for example:

  • Parent
    • child 1
      • grandchild 1
        • post 1
      • grandchild 2
        • post 2
    • child 2
      • post 3

so: all cats should be able to handle posts, and if there are only posts in the grandchildren, only list thoseā€¦ thanks! -edit- actually it should be the same as wp_list_categories, only i should be able to edit parents, children, grandchildren and post titles separately. (for example, i must be able to remove the href off the child-categories, but not the grandchild-categories..

1

1 Answers

0
votes

Try This :

   <!-- Category Archive Start -->
    <ul class="catArchive">
    <?php
    $catQuery = $wpdb->get_results("SELECT * FROM $wpdb->terms AS wterms INNER JOIN $wpdb->term_taxonomy AS wtaxonomy ON ( wterms.term_id = wtaxonomy.term_id ) WHERE wtaxonomy.taxonomy = 'category' AND wtaxonomy.parent = 0 AND wtaxonomy.count > 0");

    $catCounter = 0;

    foreach ($catQuery as $category) {

        $catCounter++;

        $catStyle = '';
        if (is_int($catCounter / 2)) $catStyle = ' class="catAlt"';

        $catLink = get_category_link($category->term_id);

        echo '<li'.$catStyle.'><h3><a href="'.$catLink.'" title="'.$category->name.'">'.$category->name.'</a></h3>';
            echo '<ul style="margin-left:15px;">';

            query_posts('category__in='.$category->term_id.'&showposts=5');?>

            <?php while (have_posts()) : the_post(); ?>
                <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
            <?php endwhile; ?>

                <li><a href="<?php echo $catLink; ?>" title="<?php echo $category->name; ?>">More <strong><?php echo $category->name; ?></strong></a></li>
         <li> <?php
         $sub_cat_id = $category->term_id;
         $get_sub_args = array('child_of' =>$sub_cat_id);
                 $categories_arr =  get_categories($get_sub_args);  
                 //print_r ($categories_arr);
        foreach  ($categories_arr as $sacategory) {
                //Display the sub category information using $category values like $category->cat_name
                echo '<h2>'.$sacategory->name.'</h2>';
                echo '<ul style="margin-left:15px;">';

                foreach (get_posts('cat='.$sacategory->term_id) as $post) {
                    setup_postdata( $post );
                    echo '<li><a href="'.get_permalink($post->ID).'">'.get_the_title().'</a></li>';   
                }  
                echo '</ul>';
            }

         ?></li>



            </ul>
        </li>
        <?php } ?>
    </ul>
    <!-- Category Archive End -->
        </div>