列表类别/子类别/孙子类别/帖子标题

时间:2015-02-17 09:35:38

标签: wordpress categories

我找到了这段代码:

<?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>';
}
?>

列出了某个类别中的所有类别和帖子。但我希望它来自所有类别。但是当我填写'child_of' => 2时,它列出了所有内容,但它没有很好地格式化。孙子与孩子具有相同的等级地位。

我想要的例子:

    • 孩子1
      • 孙子1
        • post 1
      • 孙子2
        • post 2
    • 孩子2
      • post 3

所以:所有猫应该能够处理帖子,如果孙子中只有帖子,只列出那些......谢谢! -edit-实际上它应该与wp_list_categories相同,只有我应该能够分别编辑父母,子女,孙子和帖子。 (例如,我必须能够删除子类别的href,但不能删除孙子类别..

1 个答案:

答案 0 :(得分:0)

试试这个:

   <!-- 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>